aboutsummaryrefslogtreecommitdiffstats
path: root/fs/smbfs
diff options
context:
space:
mode:
Diffstat (limited to 'fs/smbfs')
-rw-r--r--fs/smbfs/Makefile39
-rw-r--r--fs/smbfs/cache.c209
-rw-r--r--fs/smbfs/dir.c693
-rw-r--r--fs/smbfs/file.c423
-rw-r--r--fs/smbfs/getopt.c64
-rw-r--r--fs/smbfs/getopt.h14
-rw-r--r--fs/smbfs/inode.c849
-rw-r--r--fs/smbfs/ioctl.c67
-rw-r--r--fs/smbfs/proc.c3509
-rw-r--r--fs/smbfs/proto.h87
-rw-r--r--fs/smbfs/request.c823
-rw-r--r--fs/smbfs/request.h70
-rw-r--r--fs/smbfs/smb_debug.h34
-rw-r--r--fs/smbfs/smbiod.c341
-rw-r--r--fs/smbfs/sock.c388
-rw-r--r--fs/smbfs/symlink.c70
16 files changed, 7680 insertions, 0 deletions
diff --git a/fs/smbfs/Makefile b/fs/smbfs/Makefile
new file mode 100644
index 000000000000..93246b7dd6fb
--- /dev/null
+++ b/fs/smbfs/Makefile
@@ -0,0 +1,39 @@
1#
2# Makefile for the linux smb-filesystem routines.
3#
4
5obj-$(CONFIG_SMB_FS) += smbfs.o
6
7smbfs-objs := proc.o dir.o cache.o sock.o inode.o file.o ioctl.o getopt.o \
8 symlink.o smbiod.o request.o
9
10# If you want debugging output, you may add these flags to the EXTRA_CFLAGS
11# SMBFS_PARANOIA should normally be enabled.
12
13EXTRA_CFLAGS += -DSMBFS_PARANOIA
14#EXTRA_CFLAGS += -DSMBFS_DEBUG
15#EXTRA_CFLAGS += -DSMBFS_DEBUG_VERBOSE
16#EXTRA_CFLAGS += -DDEBUG_SMB_MALLOC
17#EXTRA_CFLAGS += -DDEBUG_SMB_TIMESTAMP
18#EXTRA_CFLAGS += -Werror
19
20#
21# Maintainer rules
22#
23
24# getopt.c not included. It is intentionally separate
25SRC = proc.c dir.c cache.c sock.c inode.c file.c ioctl.c smbiod.c request.c \
26 symlink.c
27
28proto:
29 -rm -f proto.h
30 @echo > proto2.h "/*"
31 @echo >> proto2.h " * Autogenerated with cproto on: " `date`
32 @echo >> proto2.h " */"
33 @echo >> proto2.h ""
34 @echo >> proto2.h "struct smb_request;"
35 @echo >> proto2.h "struct sock;"
36 @echo >> proto2.h "struct statfs;"
37 @echo >> proto2.h ""
38 cproto -E "gcc -E" -e -v -I $(TOPDIR)/include -DMAKING_PROTO -D__KERNEL__ $(SRC) >> proto2.h
39 mv proto2.h proto.h
diff --git a/fs/smbfs/cache.c b/fs/smbfs/cache.c
new file mode 100644
index 000000000000..f3e6b81288ab
--- /dev/null
+++ b/fs/smbfs/cache.c
@@ -0,0 +1,209 @@
1/*
2 * cache.c
3 *
4 * Copyright (C) 1997 by Bill Hawes
5 *
6 * Routines to support directory cacheing using the page cache.
7 * This cache code is almost directly taken from ncpfs.
8 *
9 * Please add a note about your changes to smbfs in the ChangeLog file.
10 */
11
12#include <linux/time.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/dirent.h>
17#include <linux/smb_fs.h>
18#include <linux/pagemap.h>
19#include <linux/net.h>
20
21#include <asm/page.h>
22
23#include "smb_debug.h"
24#include "proto.h"
25
26/*
27 * Force the next attempt to use the cache to be a timeout.
28 * If we can't find the page that's fine, it will cause a refresh.
29 */
30void
31smb_invalid_dir_cache(struct inode * dir)
32{
33 struct smb_sb_info *server = server_from_inode(dir);
34 union smb_dir_cache *cache = NULL;
35 struct page *page = NULL;
36
37 page = grab_cache_page(&dir->i_data, 0);
38 if (!page)
39 goto out;
40
41 if (!PageUptodate(page))
42 goto out_unlock;
43
44 cache = kmap(page);
45 cache->head.time = jiffies - SMB_MAX_AGE(server);
46
47 kunmap(page);
48 SetPageUptodate(page);
49out_unlock:
50 unlock_page(page);
51 page_cache_release(page);
52out:
53 return;
54}
55
56/*
57 * Mark all dentries for 'parent' as invalid, forcing them to be re-read
58 */
59void
60smb_invalidate_dircache_entries(struct dentry *parent)
61{
62 struct smb_sb_info *server = server_from_dentry(parent);
63 struct list_head *next;
64 struct dentry *dentry;
65
66 spin_lock(&dcache_lock);
67 next = parent->d_subdirs.next;
68 while (next != &parent->d_subdirs) {
69 dentry = list_entry(next, struct dentry, d_child);
70 dentry->d_fsdata = NULL;
71 smb_age_dentry(server, dentry);
72 next = next->next;
73 }
74 spin_unlock(&dcache_lock);
75}
76
77/*
78 * dget, but require that fpos and parent matches what the dentry contains.
79 * dentry is not known to be a valid pointer at entry.
80 */
81struct dentry *
82smb_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos)
83{
84 struct dentry *dent = dentry;
85 struct list_head *next;
86
87 if (d_validate(dent, parent)) {
88 if (dent->d_name.len <= SMB_MAXNAMELEN &&
89 (unsigned long)dent->d_fsdata == fpos) {
90 if (!dent->d_inode) {
91 dput(dent);
92 dent = NULL;
93 }
94 return dent;
95 }
96 dput(dent);
97 }
98
99 /* If a pointer is invalid, we search the dentry. */
100 spin_lock(&dcache_lock);
101 next = parent->d_subdirs.next;
102 while (next != &parent->d_subdirs) {
103 dent = list_entry(next, struct dentry, d_child);
104 if ((unsigned long)dent->d_fsdata == fpos) {
105 if (dent->d_inode)
106 dget_locked(dent);
107 else
108 dent = NULL;
109 goto out_unlock;
110 }
111 next = next->next;
112 }
113 dent = NULL;
114out_unlock:
115 spin_unlock(&dcache_lock);
116 return dent;
117}
118
119
120/*
121 * Create dentry/inode for this file and add it to the dircache.
122 */
123int
124smb_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
125 struct smb_cache_control *ctrl, struct qstr *qname,
126 struct smb_fattr *entry)
127{
128 struct dentry *newdent, *dentry = filp->f_dentry;
129 struct inode *newino, *inode = dentry->d_inode;
130 struct smb_cache_control ctl = *ctrl;
131 int valid = 0;
132 int hashed = 0;
133 ino_t ino = 0;
134
135 qname->hash = full_name_hash(qname->name, qname->len);
136
137 if (dentry->d_op && dentry->d_op->d_hash)
138 if (dentry->d_op->d_hash(dentry, qname) != 0)
139 goto end_advance;
140
141 newdent = d_lookup(dentry, qname);
142
143 if (!newdent) {
144 newdent = d_alloc(dentry, qname);
145 if (!newdent)
146 goto end_advance;
147 } else {
148 hashed = 1;
149 memcpy((char *) newdent->d_name.name, qname->name,
150 newdent->d_name.len);
151 }
152
153 if (!newdent->d_inode) {
154 smb_renew_times(newdent);
155 entry->f_ino = iunique(inode->i_sb, 2);
156 newino = smb_iget(inode->i_sb, entry);
157 if (newino) {
158 smb_new_dentry(newdent);
159 d_instantiate(newdent, newino);
160 if (!hashed)
161 d_rehash(newdent);
162 }
163 } else
164 smb_set_inode_attr(newdent->d_inode, entry);
165
166 if (newdent->d_inode) {
167 ino = newdent->d_inode->i_ino;
168 newdent->d_fsdata = (void *) ctl.fpos;
169 smb_new_dentry(newdent);
170 }
171
172 if (ctl.idx >= SMB_DIRCACHE_SIZE) {
173 if (ctl.page) {
174 kunmap(ctl.page);
175 SetPageUptodate(ctl.page);
176 unlock_page(ctl.page);
177 page_cache_release(ctl.page);
178 }
179 ctl.cache = NULL;
180 ctl.idx -= SMB_DIRCACHE_SIZE;
181 ctl.ofs += 1;
182 ctl.page = grab_cache_page(&inode->i_data, ctl.ofs);
183 if (ctl.page)
184 ctl.cache = kmap(ctl.page);
185 }
186 if (ctl.cache) {
187 ctl.cache->dentry[ctl.idx] = newdent;
188 valid = 1;
189 }
190 dput(newdent);
191
192end_advance:
193 if (!valid)
194 ctl.valid = 0;
195 if (!ctl.filled && (ctl.fpos == filp->f_pos)) {
196 if (!ino)
197 ino = find_inode_number(dentry, qname);
198 if (!ino)
199 ino = iunique(inode->i_sb, 2);
200 ctl.filled = filldir(dirent, qname->name, qname->len,
201 filp->f_pos, ino, DT_UNKNOWN);
202 if (!ctl.filled)
203 filp->f_pos += 1;
204 }
205 ctl.fpos += 1;
206 ctl.idx += 1;
207 *ctrl = ctl;
208 return (ctl.valid || !ctl.filled);
209}
diff --git a/fs/smbfs/dir.c b/fs/smbfs/dir.c
new file mode 100644
index 000000000000..c6c33e15143a
--- /dev/null
+++ b/fs/smbfs/dir.c
@@ -0,0 +1,693 @@
1/*
2 * dir.c
3 *
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/time.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/smp_lock.h>
14#include <linux/ctype.h>
15#include <linux/net.h>
16
17#include <linux/smb_fs.h>
18#include <linux/smb_mount.h>
19#include <linux/smbno.h>
20
21#include "smb_debug.h"
22#include "proto.h"
23
24static int smb_readdir(struct file *, void *, filldir_t);
25static int smb_dir_open(struct inode *, struct file *);
26
27static struct dentry *smb_lookup(struct inode *, struct dentry *, struct nameidata *);
28static int smb_create(struct inode *, struct dentry *, int, struct nameidata *);
29static int smb_mkdir(struct inode *, struct dentry *, int);
30static int smb_rmdir(struct inode *, struct dentry *);
31static int smb_unlink(struct inode *, struct dentry *);
32static int smb_rename(struct inode *, struct dentry *,
33 struct inode *, struct dentry *);
34static int smb_make_node(struct inode *,struct dentry *,int,dev_t);
35static int smb_link(struct dentry *, struct inode *, struct dentry *);
36
37struct file_operations smb_dir_operations =
38{
39 .read = generic_read_dir,
40 .readdir = smb_readdir,
41 .ioctl = smb_ioctl,
42 .open = smb_dir_open,
43};
44
45struct inode_operations smb_dir_inode_operations =
46{
47 .create = smb_create,
48 .lookup = smb_lookup,
49 .unlink = smb_unlink,
50 .mkdir = smb_mkdir,
51 .rmdir = smb_rmdir,
52 .rename = smb_rename,
53 .getattr = smb_getattr,
54 .setattr = smb_notify_change,
55};
56
57struct inode_operations smb_dir_inode_operations_unix =
58{
59 .create = smb_create,
60 .lookup = smb_lookup,
61 .unlink = smb_unlink,
62 .mkdir = smb_mkdir,
63 .rmdir = smb_rmdir,
64 .rename = smb_rename,
65 .getattr = smb_getattr,
66 .setattr = smb_notify_change,
67 .symlink = smb_symlink,
68 .mknod = smb_make_node,
69 .link = smb_link,
70};
71
72/*
73 * Read a directory, using filldir to fill the dirent memory.
74 * smb_proc_readdir does the actual reading from the smb server.
75 *
76 * The cache code is almost directly taken from ncpfs
77 */
78static int
79smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
80{
81 struct dentry *dentry = filp->f_dentry;
82 struct inode *dir = dentry->d_inode;
83 struct smb_sb_info *server = server_from_dentry(dentry);
84 union smb_dir_cache *cache = NULL;
85 struct smb_cache_control ctl;
86 struct page *page = NULL;
87 int result;
88
89 ctl.page = NULL;
90 ctl.cache = NULL;
91
92 VERBOSE("reading %s/%s, f_pos=%d\n",
93 DENTRY_PATH(dentry), (int) filp->f_pos);
94
95 result = 0;
96
97 lock_kernel();
98
99 switch ((unsigned int) filp->f_pos) {
100 case 0:
101 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
102 goto out;
103 filp->f_pos = 1;
104 /* fallthrough */
105 case 1:
106 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR) < 0)
107 goto out;
108 filp->f_pos = 2;
109 }
110
111 /*
112 * Make sure our inode is up-to-date.
113 */
114 result = smb_revalidate_inode(dentry);
115 if (result)
116 goto out;
117
118
119 page = grab_cache_page(&dir->i_data, 0);
120 if (!page)
121 goto read_really;
122
123 ctl.cache = cache = kmap(page);
124 ctl.head = cache->head;
125
126 if (!PageUptodate(page) || !ctl.head.eof) {
127 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
128 DENTRY_PATH(dentry), PageUptodate(page),ctl.head.eof);
129 goto init_cache;
130 }
131
132 if (filp->f_pos == 2) {
133 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
134 goto init_cache;
135
136 /*
137 * N.B. ncpfs checks mtime of dentry too here, we don't.
138 * 1. common smb servers do not update mtime on dir changes
139 * 2. it requires an extra smb request
140 * (revalidate has the same timeout as ctl.head.time)
141 *
142 * Instead smbfs invalidates its own cache on local changes
143 * and remote changes are not seen until timeout.
144 */
145 }
146
147 if (filp->f_pos > ctl.head.end)
148 goto finished;
149
150 ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
151 ctl.ofs = ctl.fpos / SMB_DIRCACHE_SIZE;
152 ctl.idx = ctl.fpos % SMB_DIRCACHE_SIZE;
153
154 for (;;) {
155 if (ctl.ofs != 0) {
156 ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
157 if (!ctl.page)
158 goto invalid_cache;
159 ctl.cache = kmap(ctl.page);
160 if (!PageUptodate(ctl.page))
161 goto invalid_cache;
162 }
163 while (ctl.idx < SMB_DIRCACHE_SIZE) {
164 struct dentry *dent;
165 int res;
166
167 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
168 dentry, filp->f_pos);
169 if (!dent)
170 goto invalid_cache;
171
172 res = filldir(dirent, dent->d_name.name,
173 dent->d_name.len, filp->f_pos,
174 dent->d_inode->i_ino, DT_UNKNOWN);
175 dput(dent);
176 if (res)
177 goto finished;
178 filp->f_pos += 1;
179 ctl.idx += 1;
180 if (filp->f_pos > ctl.head.end)
181 goto finished;
182 }
183 if (ctl.page) {
184 kunmap(ctl.page);
185 SetPageUptodate(ctl.page);
186 unlock_page(ctl.page);
187 page_cache_release(ctl.page);
188 ctl.page = NULL;
189 }
190 ctl.idx = 0;
191 ctl.ofs += 1;
192 }
193invalid_cache:
194 if (ctl.page) {
195 kunmap(ctl.page);
196 unlock_page(ctl.page);
197 page_cache_release(ctl.page);
198 ctl.page = NULL;
199 }
200 ctl.cache = cache;
201init_cache:
202 smb_invalidate_dircache_entries(dentry);
203 ctl.head.time = jiffies;
204 ctl.head.eof = 0;
205 ctl.fpos = 2;
206 ctl.ofs = 0;
207 ctl.idx = SMB_DIRCACHE_START;
208 ctl.filled = 0;
209 ctl.valid = 1;
210read_really:
211 result = server->ops->readdir(filp, dirent, filldir, &ctl);
212 if (ctl.idx == -1)
213 goto invalid_cache; /* retry */
214 ctl.head.end = ctl.fpos - 1;
215 ctl.head.eof = ctl.valid;
216finished:
217 if (page) {
218 cache->head = ctl.head;
219 kunmap(page);
220 SetPageUptodate(page);
221 unlock_page(page);
222 page_cache_release(page);
223 }
224 if (ctl.page) {
225 kunmap(ctl.page);
226 SetPageUptodate(ctl.page);
227 unlock_page(ctl.page);
228 page_cache_release(ctl.page);
229 }
230out:
231 unlock_kernel();
232 return result;
233}
234
235static int
236smb_dir_open(struct inode *dir, struct file *file)
237{
238 struct dentry *dentry = file->f_dentry;
239 struct smb_sb_info *server;
240 int error = 0;
241
242 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
243 file->f_dentry->d_name.name);
244
245 /*
246 * Directory timestamps in the core protocol aren't updated
247 * when a file is added, so we give them a very short TTL.
248 */
249 lock_kernel();
250 server = server_from_dentry(dentry);
251 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
252 unsigned long age = jiffies - SMB_I(dir)->oldmtime;
253 if (age > 2*HZ)
254 smb_invalid_dir_cache(dir);
255 }
256
257 /*
258 * Note: in order to allow the smbmount process to open the
259 * mount point, we only revalidate if the connection is valid or
260 * if the process is trying to access something other than the root.
261 */
262 if (server->state == CONN_VALID || !IS_ROOT(dentry))
263 error = smb_revalidate_inode(dentry);
264 unlock_kernel();
265 return error;
266}
267
268/*
269 * Dentry operations routines
270 */
271static int smb_lookup_validate(struct dentry *, struct nameidata *);
272static int smb_hash_dentry(struct dentry *, struct qstr *);
273static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
274static int smb_delete_dentry(struct dentry *);
275
276static struct dentry_operations smbfs_dentry_operations =
277{
278 .d_revalidate = smb_lookup_validate,
279 .d_hash = smb_hash_dentry,
280 .d_compare = smb_compare_dentry,
281 .d_delete = smb_delete_dentry,
282};
283
284static struct dentry_operations smbfs_dentry_operations_case =
285{
286 .d_revalidate = smb_lookup_validate,
287 .d_delete = smb_delete_dentry,
288};
289
290
291/*
292 * This is the callback when the dcache has a lookup hit.
293 */
294static int
295smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
296{
297 struct smb_sb_info *server = server_from_dentry(dentry);
298 struct inode * inode = dentry->d_inode;
299 unsigned long age = jiffies - dentry->d_time;
300 int valid;
301
302 /*
303 * The default validation is based on dentry age:
304 * we believe in dentries for a few seconds. (But each
305 * successful server lookup renews the timestamp.)
306 */
307 valid = (age <= SMB_MAX_AGE(server));
308#ifdef SMBFS_DEBUG_VERBOSE
309 if (!valid)
310 VERBOSE("%s/%s not valid, age=%lu\n",
311 DENTRY_PATH(dentry), age);
312#endif
313
314 if (inode) {
315 lock_kernel();
316 if (is_bad_inode(inode)) {
317 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
318 valid = 0;
319 } else if (!valid)
320 valid = (smb_revalidate_inode(dentry) == 0);
321 unlock_kernel();
322 } else {
323 /*
324 * What should we do for negative dentries?
325 */
326 }
327 return valid;
328}
329
330static int
331smb_hash_dentry(struct dentry *dir, struct qstr *this)
332{
333 unsigned long hash;
334 int i;
335
336 hash = init_name_hash();
337 for (i=0; i < this->len ; i++)
338 hash = partial_name_hash(tolower(this->name[i]), hash);
339 this->hash = end_name_hash(hash);
340
341 return 0;
342}
343
344static int
345smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
346{
347 int i, result = 1;
348
349 if (a->len != b->len)
350 goto out;
351 for (i=0; i < a->len; i++) {
352 if (tolower(a->name[i]) != tolower(b->name[i]))
353 goto out;
354 }
355 result = 0;
356out:
357 return result;
358}
359
360/*
361 * This is the callback from dput() when d_count is going to 0.
362 * We use this to unhash dentries with bad inodes.
363 */
364static int
365smb_delete_dentry(struct dentry * dentry)
366{
367 if (dentry->d_inode) {
368 if (is_bad_inode(dentry->d_inode)) {
369 PARANOIA("bad inode, unhashing %s/%s\n",
370 DENTRY_PATH(dentry));
371 return 1;
372 }
373 } else {
374 /* N.B. Unhash negative dentries? */
375 }
376 return 0;
377}
378
379/*
380 * Initialize a new dentry
381 */
382void
383smb_new_dentry(struct dentry *dentry)
384{
385 struct smb_sb_info *server = server_from_dentry(dentry);
386
387 if (server->mnt->flags & SMB_MOUNT_CASE)
388 dentry->d_op = &smbfs_dentry_operations_case;
389 else
390 dentry->d_op = &smbfs_dentry_operations;
391 dentry->d_time = jiffies;
392}
393
394
395/*
396 * Whenever a lookup succeeds, we know the parent directories
397 * are all valid, so we want to update the dentry timestamps.
398 * N.B. Move this to dcache?
399 */
400void
401smb_renew_times(struct dentry * dentry)
402{
403 dget(dentry);
404 spin_lock(&dentry->d_lock);
405 for (;;) {
406 struct dentry *parent;
407
408 dentry->d_time = jiffies;
409 if (IS_ROOT(dentry))
410 break;
411 parent = dentry->d_parent;
412 dget(parent);
413 spin_unlock(&dentry->d_lock);
414 dput(dentry);
415 dentry = parent;
416 spin_lock(&dentry->d_lock);
417 }
418 spin_unlock(&dentry->d_lock);
419 dput(dentry);
420}
421
422static struct dentry *
423smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
424{
425 struct smb_fattr finfo;
426 struct inode *inode;
427 int error;
428 struct smb_sb_info *server;
429
430 error = -ENAMETOOLONG;
431 if (dentry->d_name.len > SMB_MAXNAMELEN)
432 goto out;
433
434 lock_kernel();
435 error = smb_proc_getattr(dentry, &finfo);
436#ifdef SMBFS_PARANOIA
437 if (error && error != -ENOENT)
438 PARANOIA("find %s/%s failed, error=%d\n",
439 DENTRY_PATH(dentry), error);
440#endif
441
442 inode = NULL;
443 if (error == -ENOENT)
444 goto add_entry;
445 if (!error) {
446 error = -EACCES;
447 finfo.f_ino = iunique(dentry->d_sb, 2);
448 inode = smb_iget(dir->i_sb, &finfo);
449 if (inode) {
450 add_entry:
451 server = server_from_dentry(dentry);
452 if (server->mnt->flags & SMB_MOUNT_CASE)
453 dentry->d_op = &smbfs_dentry_operations_case;
454 else
455 dentry->d_op = &smbfs_dentry_operations;
456
457 d_add(dentry, inode);
458 smb_renew_times(dentry);
459 error = 0;
460 }
461 }
462 unlock_kernel();
463out:
464 return ERR_PTR(error);
465}
466
467/*
468 * This code is common to all routines creating a new inode.
469 */
470static int
471smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
472{
473 struct smb_sb_info *server = server_from_dentry(dentry);
474 struct inode *inode;
475 int error;
476 struct smb_fattr fattr;
477
478 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
479
480 error = smb_proc_getattr(dentry, &fattr);
481 if (error)
482 goto out_close;
483
484 smb_renew_times(dentry);
485 fattr.f_ino = iunique(dentry->d_sb, 2);
486 inode = smb_iget(dentry->d_sb, &fattr);
487 if (!inode)
488 goto out_no_inode;
489
490 if (have_id) {
491 struct smb_inode_info *ei = SMB_I(inode);
492 ei->fileid = fileid;
493 ei->access = SMB_O_RDWR;
494 ei->open = server->generation;
495 }
496 d_instantiate(dentry, inode);
497out:
498 return error;
499
500out_no_inode:
501 error = -EACCES;
502out_close:
503 if (have_id) {
504 PARANOIA("%s/%s failed, error=%d, closing %u\n",
505 DENTRY_PATH(dentry), error, fileid);
506 smb_close_fileid(dentry, fileid);
507 }
508 goto out;
509}
510
511/* N.B. How should the mode argument be used? */
512static int
513smb_create(struct inode *dir, struct dentry *dentry, int mode,
514 struct nameidata *nd)
515{
516 struct smb_sb_info *server = server_from_dentry(dentry);
517 __u16 fileid;
518 int error;
519 struct iattr attr;
520
521 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
522
523 lock_kernel();
524 smb_invalid_dir_cache(dir);
525 error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
526 if (!error) {
527 if (server->opt.capabilities & SMB_CAP_UNIX) {
528 /* Set attributes for new file */
529 attr.ia_valid = ATTR_MODE;
530 attr.ia_mode = mode;
531 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
532 }
533 error = smb_instantiate(dentry, fileid, 1);
534 } else {
535 PARANOIA("%s/%s failed, error=%d\n",
536 DENTRY_PATH(dentry), error);
537 }
538 unlock_kernel();
539 return error;
540}
541
542/* N.B. How should the mode argument be used? */
543static int
544smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
545{
546 struct smb_sb_info *server = server_from_dentry(dentry);
547 int error;
548 struct iattr attr;
549
550 lock_kernel();
551 smb_invalid_dir_cache(dir);
552 error = smb_proc_mkdir(dentry);
553 if (!error) {
554 if (server->opt.capabilities & SMB_CAP_UNIX) {
555 /* Set attributes for new directory */
556 attr.ia_valid = ATTR_MODE;
557 attr.ia_mode = mode;
558 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
559 }
560 error = smb_instantiate(dentry, 0, 0);
561 }
562 unlock_kernel();
563 return error;
564}
565
566static int
567smb_rmdir(struct inode *dir, struct dentry *dentry)
568{
569 struct inode *inode = dentry->d_inode;
570 int error;
571
572 /*
573 * Close the directory if it's open.
574 */
575 lock_kernel();
576 smb_close(inode);
577
578 /*
579 * Check that nobody else is using the directory..
580 */
581 error = -EBUSY;
582 if (!d_unhashed(dentry))
583 goto out;
584
585 smb_invalid_dir_cache(dir);
586 error = smb_proc_rmdir(dentry);
587
588out:
589 unlock_kernel();
590 return error;
591}
592
593static int
594smb_unlink(struct inode *dir, struct dentry *dentry)
595{
596 int error;
597
598 /*
599 * Close the file if it's open.
600 */
601 lock_kernel();
602 smb_close(dentry->d_inode);
603
604 smb_invalid_dir_cache(dir);
605 error = smb_proc_unlink(dentry);
606 if (!error)
607 smb_renew_times(dentry);
608 unlock_kernel();
609 return error;
610}
611
612static int
613smb_rename(struct inode *old_dir, struct dentry *old_dentry,
614 struct inode *new_dir, struct dentry *new_dentry)
615{
616 int error;
617
618 /*
619 * Close any open files, and check whether to delete the
620 * target before attempting the rename.
621 */
622 lock_kernel();
623 if (old_dentry->d_inode)
624 smb_close(old_dentry->d_inode);
625 if (new_dentry->d_inode) {
626 smb_close(new_dentry->d_inode);
627 error = smb_proc_unlink(new_dentry);
628 if (error) {
629 VERBOSE("unlink %s/%s, error=%d\n",
630 DENTRY_PATH(new_dentry), error);
631 goto out;
632 }
633 /* FIXME */
634 d_delete(new_dentry);
635 }
636
637 smb_invalid_dir_cache(old_dir);
638 smb_invalid_dir_cache(new_dir);
639 error = smb_proc_mv(old_dentry, new_dentry);
640 if (!error) {
641 smb_renew_times(old_dentry);
642 smb_renew_times(new_dentry);
643 }
644out:
645 unlock_kernel();
646 return error;
647}
648
649/*
650 * FIXME: samba servers won't let you create device nodes unless uid/gid
651 * matches the connection credentials (and we don't know which those are ...)
652 */
653static int
654smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
655{
656 int error;
657 struct iattr attr;
658
659 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
660 attr.ia_mode = mode;
661 attr.ia_uid = current->euid;
662 attr.ia_gid = current->egid;
663
664 if (!new_valid_dev(dev))
665 return -EINVAL;
666
667 smb_invalid_dir_cache(dir);
668 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
669 if (!error) {
670 error = smb_instantiate(dentry, 0, 0);
671 }
672 return error;
673}
674
675/*
676 * dentry = existing file
677 * new_dentry = new file
678 */
679static int
680smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
681{
682 int error;
683
684 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
685 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
686 smb_invalid_dir_cache(dir);
687 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
688 if (!error) {
689 smb_renew_times(dentry);
690 error = smb_instantiate(new_dentry, 0, 0);
691 }
692 return error;
693}
diff --git a/fs/smbfs/file.c b/fs/smbfs/file.c
new file mode 100644
index 000000000000..b4fcfa8b55a1
--- /dev/null
+++ b/fs/smbfs/file.c
@@ -0,0 +1,423 @@
1/*
2 * file.c
3 *
4 * Copyright (C) 1995, 1996, 1997 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/time.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/fcntl.h>
14#include <linux/stat.h>
15#include <linux/mm.h>
16#include <linux/slab.h>
17#include <linux/pagemap.h>
18#include <linux/smp_lock.h>
19#include <linux/net.h>
20
21#include <asm/uaccess.h>
22#include <asm/system.h>
23
24#include <linux/smbno.h>
25#include <linux/smb_fs.h>
26
27#include "smb_debug.h"
28#include "proto.h"
29
30static int
31smb_fsync(struct file *file, struct dentry * dentry, int datasync)
32{
33 struct smb_sb_info *server = server_from_dentry(dentry);
34 int result;
35
36 VERBOSE("sync file %s/%s\n", DENTRY_PATH(dentry));
37
38 /*
39 * The VFS will writepage() all dirty pages for us, but we
40 * should send a SMBflush to the server, letting it know that
41 * we want things synchronized with actual storage.
42 *
43 * Note: this function requires all pages to have been written already
44 * (should be ok with writepage_sync)
45 */
46 result = smb_proc_flush(server, SMB_I(dentry->d_inode)->fileid);
47 return result;
48}
49
50/*
51 * Read a page synchronously.
52 */
53static int
54smb_readpage_sync(struct dentry *dentry, struct page *page)
55{
56 char *buffer = kmap(page);
57 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
58 struct smb_sb_info *server = server_from_dentry(dentry);
59 unsigned int rsize = smb_get_rsize(server);
60 int count = PAGE_SIZE;
61 int result;
62
63 VERBOSE("file %s/%s, count=%d@%Ld, rsize=%d\n",
64 DENTRY_PATH(dentry), count, offset, rsize);
65
66 result = smb_open(dentry, SMB_O_RDONLY);
67 if (result < 0)
68 goto io_error;
69
70 do {
71 if (count < rsize)
72 rsize = count;
73
74 result = server->ops->read(dentry->d_inode,offset,rsize,buffer);
75 if (result < 0)
76 goto io_error;
77
78 count -= result;
79 offset += result;
80 buffer += result;
81 dentry->d_inode->i_atime =
82 current_fs_time(dentry->d_inode->i_sb);
83 if (result < rsize)
84 break;
85 } while (count);
86
87 memset(buffer, 0, count);
88 flush_dcache_page(page);
89 SetPageUptodate(page);
90 result = 0;
91
92io_error:
93 kunmap(page);
94 unlock_page(page);
95 return result;
96}
97
98/*
99 * We are called with the page locked and we unlock it when done.
100 */
101static int
102smb_readpage(struct file *file, struct page *page)
103{
104 int error;
105 struct dentry *dentry = file->f_dentry;
106
107 page_cache_get(page);
108 error = smb_readpage_sync(dentry, page);
109 page_cache_release(page);
110 return error;
111}
112
113/*
114 * Write a page synchronously.
115 * Offset is the data offset within the page.
116 */
117static int
118smb_writepage_sync(struct inode *inode, struct page *page,
119 unsigned long pageoffset, unsigned int count)
120{
121 loff_t offset;
122 char *buffer = kmap(page) + pageoffset;
123 struct smb_sb_info *server = server_from_inode(inode);
124 unsigned int wsize = smb_get_wsize(server);
125 int ret = 0;
126
127 offset = ((loff_t)page->index << PAGE_CACHE_SHIFT) + pageoffset;
128 VERBOSE("file ino=%ld, fileid=%d, count=%d@%Ld, wsize=%d\n",
129 inode->i_ino, SMB_I(inode)->fileid, count, offset, wsize);
130
131 do {
132 int write_ret;
133
134 if (count < wsize)
135 wsize = count;
136
137 write_ret = server->ops->write(inode, offset, wsize, buffer);
138 if (write_ret < 0) {
139 PARANOIA("failed write, wsize=%d, write_ret=%d\n",
140 wsize, write_ret);
141 ret = write_ret;
142 break;
143 }
144 /* N.B. what if result < wsize?? */
145#ifdef SMBFS_PARANOIA
146 if (write_ret < wsize)
147 PARANOIA("short write, wsize=%d, write_ret=%d\n",
148 wsize, write_ret);
149#endif
150 buffer += wsize;
151 offset += wsize;
152 count -= wsize;
153 /*
154 * Update the inode now rather than waiting for a refresh.
155 */
156 inode->i_mtime = inode->i_atime = current_fs_time(inode->i_sb);
157 SMB_I(inode)->flags |= SMB_F_LOCALWRITE;
158 if (offset > inode->i_size)
159 inode->i_size = offset;
160 } while (count);
161
162 kunmap(page);
163 return ret;
164}
165
166/*
167 * Write a page to the server. This will be used for NFS swapping only
168 * (for now), and we currently do this synchronously only.
169 *
170 * We are called with the page locked and we unlock it when done.
171 */
172static int
173smb_writepage(struct page *page, struct writeback_control *wbc)
174{
175 struct address_space *mapping = page->mapping;
176 struct inode *inode;
177 unsigned long end_index;
178 unsigned offset = PAGE_CACHE_SIZE;
179 int err;
180
181 if (!mapping)
182 BUG();
183 inode = mapping->host;
184 if (!inode)
185 BUG();
186
187 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
188
189 /* easy case */
190 if (page->index < end_index)
191 goto do_it;
192 /* things got complicated... */
193 offset = inode->i_size & (PAGE_CACHE_SIZE-1);
194 /* OK, are we completely out? */
195 if (page->index >= end_index+1 || !offset)
196 return 0; /* truncated - don't care */
197do_it:
198 page_cache_get(page);
199 err = smb_writepage_sync(inode, page, 0, offset);
200 SetPageUptodate(page);
201 unlock_page(page);
202 page_cache_release(page);
203 return err;
204}
205
206static int
207smb_updatepage(struct file *file, struct page *page, unsigned long offset,
208 unsigned int count)
209{
210 struct dentry *dentry = file->f_dentry;
211
212 DEBUG1("(%s/%s %d@%ld)\n", DENTRY_PATH(dentry),
213 count, (page->index << PAGE_CACHE_SHIFT)+offset);
214
215 return smb_writepage_sync(dentry->d_inode, page, offset, count);
216}
217
218static ssize_t
219smb_file_read(struct file * file, char __user * buf, size_t count, loff_t *ppos)
220{
221 struct dentry * dentry = file->f_dentry;
222 ssize_t status;
223
224 VERBOSE("file %s/%s, count=%lu@%lu\n", DENTRY_PATH(dentry),
225 (unsigned long) count, (unsigned long) *ppos);
226
227 status = smb_revalidate_inode(dentry);
228 if (status) {
229 PARANOIA("%s/%s validation failed, error=%Zd\n",
230 DENTRY_PATH(dentry), status);
231 goto out;
232 }
233
234 VERBOSE("before read, size=%ld, flags=%x, atime=%ld\n",
235 (long)dentry->d_inode->i_size,
236 dentry->d_inode->i_flags, dentry->d_inode->i_atime);
237
238 status = generic_file_read(file, buf, count, ppos);
239out:
240 return status;
241}
242
243static int
244smb_file_mmap(struct file * file, struct vm_area_struct * vma)
245{
246 struct dentry * dentry = file->f_dentry;
247 int status;
248
249 VERBOSE("file %s/%s, address %lu - %lu\n",
250 DENTRY_PATH(dentry), vma->vm_start, vma->vm_end);
251
252 status = smb_revalidate_inode(dentry);
253 if (status) {
254 PARANOIA("%s/%s validation failed, error=%d\n",
255 DENTRY_PATH(dentry), status);
256 goto out;
257 }
258 status = generic_file_mmap(file, vma);
259out:
260 return status;
261}
262
263static ssize_t
264smb_file_sendfile(struct file *file, loff_t *ppos,
265 size_t count, read_actor_t actor, void *target)
266{
267 struct dentry *dentry = file->f_dentry;
268 ssize_t status;
269
270 VERBOSE("file %s/%s, pos=%Ld, count=%d\n",
271 DENTRY_PATH(dentry), *ppos, count);
272
273 status = smb_revalidate_inode(dentry);
274 if (status) {
275 PARANOIA("%s/%s validation failed, error=%Zd\n",
276 DENTRY_PATH(dentry), status);
277 goto out;
278 }
279 status = generic_file_sendfile(file, ppos, count, actor, target);
280out:
281 return status;
282}
283
284/*
285 * This does the "real" work of the write. The generic routine has
286 * allocated the page, locked it, done all the page alignment stuff
287 * calculations etc. Now we should just copy the data from user
288 * space and write it back to the real medium..
289 *
290 * If the writer ends up delaying the write, the writer needs to
291 * increment the page use counts until he is done with the page.
292 */
293static int smb_prepare_write(struct file *file, struct page *page,
294 unsigned offset, unsigned to)
295{
296 return 0;
297}
298
299static int smb_commit_write(struct file *file, struct page *page,
300 unsigned offset, unsigned to)
301{
302 int status;
303
304 status = -EFAULT;
305 lock_kernel();
306 status = smb_updatepage(file, page, offset, to-offset);
307 unlock_kernel();
308 return status;
309}
310
311struct address_space_operations smb_file_aops = {
312 .readpage = smb_readpage,
313 .writepage = smb_writepage,
314 .prepare_write = smb_prepare_write,
315 .commit_write = smb_commit_write
316};
317
318/*
319 * Write to a file (through the page cache).
320 */
321static ssize_t
322smb_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
323{
324 struct dentry * dentry = file->f_dentry;
325 ssize_t result;
326
327 VERBOSE("file %s/%s, count=%lu@%lu\n",
328 DENTRY_PATH(dentry),
329 (unsigned long) count, (unsigned long) *ppos);
330
331 result = smb_revalidate_inode(dentry);
332 if (result) {
333 PARANOIA("%s/%s validation failed, error=%Zd\n",
334 DENTRY_PATH(dentry), result);
335 goto out;
336 }
337
338 result = smb_open(dentry, SMB_O_WRONLY);
339 if (result)
340 goto out;
341
342 if (count > 0) {
343 result = generic_file_write(file, buf, count, ppos);
344 VERBOSE("pos=%ld, size=%ld, mtime=%ld, atime=%ld\n",
345 (long) file->f_pos, (long) dentry->d_inode->i_size,
346 dentry->d_inode->i_mtime, dentry->d_inode->i_atime);
347 }
348out:
349 return result;
350}
351
352static int
353smb_file_open(struct inode *inode, struct file * file)
354{
355 int result;
356 struct dentry *dentry = file->f_dentry;
357 int smb_mode = (file->f_mode & O_ACCMODE) - 1;
358
359 lock_kernel();
360 result = smb_open(dentry, smb_mode);
361 if (result)
362 goto out;
363 SMB_I(inode)->openers++;
364out:
365 unlock_kernel();
366 return result;
367}
368
369static int
370smb_file_release(struct inode *inode, struct file * file)
371{
372 lock_kernel();
373 if (!--SMB_I(inode)->openers) {
374 /* We must flush any dirty pages now as we won't be able to
375 write anything after close. mmap can trigger this.
376 "openers" should perhaps include mmap'ers ... */
377 filemap_fdatawrite(inode->i_mapping);
378 filemap_fdatawait(inode->i_mapping);
379 smb_close(inode);
380 }
381 unlock_kernel();
382 return 0;
383}
384
385/*
386 * Check whether the required access is compatible with
387 * an inode's permission. SMB doesn't recognize superuser
388 * privileges, so we need our own check for this.
389 */
390static int
391smb_file_permission(struct inode *inode, int mask, struct nameidata *nd)
392{
393 int mode = inode->i_mode;
394 int error = 0;
395
396 VERBOSE("mode=%x, mask=%x\n", mode, mask);
397
398 /* Look at user permissions */
399 mode >>= 6;
400 if ((mode & 7 & mask) != mask)
401 error = -EACCES;
402 return error;
403}
404
405struct file_operations smb_file_operations =
406{
407 .llseek = remote_llseek,
408 .read = smb_file_read,
409 .write = smb_file_write,
410 .ioctl = smb_ioctl,
411 .mmap = smb_file_mmap,
412 .open = smb_file_open,
413 .release = smb_file_release,
414 .fsync = smb_fsync,
415 .sendfile = smb_file_sendfile,
416};
417
418struct inode_operations smb_file_inode_operations =
419{
420 .permission = smb_file_permission,
421 .getattr = smb_getattr,
422 .setattr = smb_notify_change,
423};
diff --git a/fs/smbfs/getopt.c b/fs/smbfs/getopt.c
new file mode 100644
index 000000000000..7ae0f5273ab1
--- /dev/null
+++ b/fs/smbfs/getopt.c
@@ -0,0 +1,64 @@
1/*
2 * getopt.c
3 */
4
5#include <linux/kernel.h>
6#include <linux/string.h>
7#include <linux/net.h>
8
9#include "getopt.h"
10
11/**
12 * smb_getopt - option parser
13 * @caller: name of the caller, for error messages
14 * @options: the options string
15 * @opts: an array of &struct option entries controlling parser operations
16 * @optopt: output; will contain the current option
17 * @optarg: output; will contain the value (if one exists)
18 * @flag: output; may be NULL; should point to a long for or'ing flags
19 * @value: output; may be NULL; will be overwritten with the integer value
20 * of the current argument.
21 *
22 * Helper to parse options on the format used by mount ("a=b,c=d,e,f").
23 * Returns opts->val if a matching entry in the 'opts' array is found,
24 * 0 when no more tokens are found, -1 if an error is encountered.
25 */
26int smb_getopt(char *caller, char **options, struct option *opts,
27 char **optopt, char **optarg, unsigned long *flag,
28 unsigned long *value)
29{
30 char *token;
31 char *val;
32 int i;
33
34 do {
35 if ((token = strsep(options, ",")) == NULL)
36 return 0;
37 } while (*token == '\0');
38 *optopt = token;
39
40 *optarg = NULL;
41 if ((val = strchr (token, '=')) != NULL) {
42 *val++ = 0;
43 if (value)
44 *value = simple_strtoul(val, NULL, 0);
45 *optarg = val;
46 }
47
48 for (i = 0; opts[i].name != NULL; i++) {
49 if (!strcmp(opts[i].name, token)) {
50 if (!opts[i].flag && (!val || !*val)) {
51 printk("%s: the %s option requires an argument\n",
52 caller, token);
53 return -1;
54 }
55
56 if (flag && opts[i].flag)
57 *flag |= opts[i].flag;
58
59 return opts[i].val;
60 }
61 }
62 printk("%s: Unrecognized mount option %s\n", caller, token);
63 return -1;
64}
diff --git a/fs/smbfs/getopt.h b/fs/smbfs/getopt.h
new file mode 100644
index 000000000000..146219ac7c46
--- /dev/null
+++ b/fs/smbfs/getopt.h
@@ -0,0 +1,14 @@
1#ifndef _LINUX_GETOPT_H
2#define _LINUX_GETOPT_H
3
4struct option {
5 const char *name;
6 unsigned long flag;
7 int val;
8};
9
10extern int smb_getopt(char *caller, char **options, struct option *opts,
11 char **optopt, char **optarg, unsigned long *flag,
12 unsigned long *value);
13
14#endif /* _LINUX_GETOPT_H */
diff --git a/fs/smbfs/inode.c b/fs/smbfs/inode.c
new file mode 100644
index 000000000000..4765aaac9fd2
--- /dev/null
+++ b/fs/smbfs/inode.c
@@ -0,0 +1,849 @@
1/*
2 * inode.c
3 *
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/config.h>
11#include <linux/module.h>
12#include <linux/time.h>
13#include <linux/kernel.h>
14#include <linux/mm.h>
15#include <linux/string.h>
16#include <linux/stat.h>
17#include <linux/errno.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/file.h>
21#include <linux/dcache.h>
22#include <linux/smp_lock.h>
23#include <linux/nls.h>
24#include <linux/seq_file.h>
25#include <linux/mount.h>
26#include <linux/net.h>
27#include <linux/vfs.h>
28#include <linux/highuid.h>
29#include <linux/smb_fs.h>
30#include <linux/smbno.h>
31#include <linux/smb_mount.h>
32
33#include <asm/system.h>
34#include <asm/uaccess.h>
35
36#include "smb_debug.h"
37#include "getopt.h"
38#include "proto.h"
39
40/* Always pick a default string */
41#ifdef CONFIG_SMB_NLS_REMOTE
42#define SMB_NLS_REMOTE CONFIG_SMB_NLS_REMOTE
43#else
44#define SMB_NLS_REMOTE ""
45#endif
46
47#define SMB_TTL_DEFAULT 1000
48
49static void smb_delete_inode(struct inode *);
50static void smb_put_super(struct super_block *);
51static int smb_statfs(struct super_block *, struct kstatfs *);
52static int smb_show_options(struct seq_file *, struct vfsmount *);
53
54static kmem_cache_t *smb_inode_cachep;
55
56static struct inode *smb_alloc_inode(struct super_block *sb)
57{
58 struct smb_inode_info *ei;
59 ei = (struct smb_inode_info *)kmem_cache_alloc(smb_inode_cachep, SLAB_KERNEL);
60 if (!ei)
61 return NULL;
62 return &ei->vfs_inode;
63}
64
65static void smb_destroy_inode(struct inode *inode)
66{
67 kmem_cache_free(smb_inode_cachep, SMB_I(inode));
68}
69
70static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
71{
72 struct smb_inode_info *ei = (struct smb_inode_info *) foo;
73 unsigned long flagmask = SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR;
74
75 if ((flags & flagmask) == SLAB_CTOR_CONSTRUCTOR)
76 inode_init_once(&ei->vfs_inode);
77}
78
79static int init_inodecache(void)
80{
81 smb_inode_cachep = kmem_cache_create("smb_inode_cache",
82 sizeof(struct smb_inode_info),
83 0, SLAB_RECLAIM_ACCOUNT,
84 init_once, NULL);
85 if (smb_inode_cachep == NULL)
86 return -ENOMEM;
87 return 0;
88}
89
90static void destroy_inodecache(void)
91{
92 if (kmem_cache_destroy(smb_inode_cachep))
93 printk(KERN_INFO "smb_inode_cache: not all structures were freed\n");
94}
95
96static int smb_remount(struct super_block *sb, int *flags, char *data)
97{
98 *flags |= MS_NODIRATIME;
99 return 0;
100}
101
102static struct super_operations smb_sops =
103{
104 .alloc_inode = smb_alloc_inode,
105 .destroy_inode = smb_destroy_inode,
106 .drop_inode = generic_delete_inode,
107 .delete_inode = smb_delete_inode,
108 .put_super = smb_put_super,
109 .statfs = smb_statfs,
110 .show_options = smb_show_options,
111 .remount_fs = smb_remount,
112};
113
114
115/* We are always generating a new inode here */
116struct inode *
117smb_iget(struct super_block *sb, struct smb_fattr *fattr)
118{
119 struct smb_sb_info *server = SMB_SB(sb);
120 struct inode *result;
121
122 DEBUG1("smb_iget: %p\n", fattr);
123
124 result = new_inode(sb);
125 if (!result)
126 return result;
127 result->i_ino = fattr->f_ino;
128 SMB_I(result)->open = 0;
129 SMB_I(result)->fileid = 0;
130 SMB_I(result)->access = 0;
131 SMB_I(result)->flags = 0;
132 SMB_I(result)->closed = 0;
133 SMB_I(result)->openers = 0;
134 smb_set_inode_attr(result, fattr);
135 if (S_ISREG(result->i_mode)) {
136 result->i_op = &smb_file_inode_operations;
137 result->i_fop = &smb_file_operations;
138 result->i_data.a_ops = &smb_file_aops;
139 } else if (S_ISDIR(result->i_mode)) {
140 if (server->opt.capabilities & SMB_CAP_UNIX)
141 result->i_op = &smb_dir_inode_operations_unix;
142 else
143 result->i_op = &smb_dir_inode_operations;
144 result->i_fop = &smb_dir_operations;
145 } else if (S_ISLNK(result->i_mode)) {
146 result->i_op = &smb_link_inode_operations;
147 } else {
148 init_special_inode(result, result->i_mode, fattr->f_rdev);
149 }
150 insert_inode_hash(result);
151 return result;
152}
153
154/*
155 * Copy the inode data to a smb_fattr structure.
156 */
157void
158smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr)
159{
160 memset(fattr, 0, sizeof(struct smb_fattr));
161 fattr->f_mode = inode->i_mode;
162 fattr->f_nlink = inode->i_nlink;
163 fattr->f_ino = inode->i_ino;
164 fattr->f_uid = inode->i_uid;
165 fattr->f_gid = inode->i_gid;
166 fattr->f_size = inode->i_size;
167 fattr->f_mtime = inode->i_mtime;
168 fattr->f_ctime = inode->i_ctime;
169 fattr->f_atime = inode->i_atime;
170 fattr->f_blksize= inode->i_blksize;
171 fattr->f_blocks = inode->i_blocks;
172
173 fattr->attr = SMB_I(inode)->attr;
174 /*
175 * Keep the attributes in sync with the inode permissions.
176 */
177 if (fattr->f_mode & S_IWUSR)
178 fattr->attr &= ~aRONLY;
179 else
180 fattr->attr |= aRONLY;
181}
182
183/*
184 * Update the inode, possibly causing it to invalidate its pages if mtime/size
185 * is different from last time.
186 */
187void
188smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr)
189{
190 struct smb_inode_info *ei = SMB_I(inode);
191
192 /*
193 * A size change should have a different mtime, or same mtime
194 * but different size.
195 */
196 time_t last_time = inode->i_mtime.tv_sec;
197 loff_t last_sz = inode->i_size;
198
199 inode->i_mode = fattr->f_mode;
200 inode->i_nlink = fattr->f_nlink;
201 inode->i_uid = fattr->f_uid;
202 inode->i_gid = fattr->f_gid;
203 inode->i_ctime = fattr->f_ctime;
204 inode->i_blksize= fattr->f_blksize;
205 inode->i_blocks = fattr->f_blocks;
206 inode->i_size = fattr->f_size;
207 inode->i_mtime = fattr->f_mtime;
208 inode->i_atime = fattr->f_atime;
209 ei->attr = fattr->attr;
210
211 /*
212 * Update the "last time refreshed" field for revalidation.
213 */
214 ei->oldmtime = jiffies;
215
216 if (inode->i_mtime.tv_sec != last_time || inode->i_size != last_sz) {
217 VERBOSE("%ld changed, old=%ld, new=%ld, oz=%ld, nz=%ld\n",
218 inode->i_ino,
219 (long) last_time, (long) inode->i_mtime,
220 (long) last_sz, (long) inode->i_size);
221
222 if (!S_ISDIR(inode->i_mode))
223 invalidate_remote_inode(inode);
224 }
225}
226
227/*
228 * This is called if the connection has gone bad ...
229 * try to kill off all the current inodes.
230 */
231void
232smb_invalidate_inodes(struct smb_sb_info *server)
233{
234 VERBOSE("\n");
235 shrink_dcache_sb(SB_of(server));
236 invalidate_inodes(SB_of(server));
237}
238
239/*
240 * This is called to update the inode attributes after
241 * we've made changes to a file or directory.
242 */
243static int
244smb_refresh_inode(struct dentry *dentry)
245{
246 struct inode *inode = dentry->d_inode;
247 int error;
248 struct smb_fattr fattr;
249
250 error = smb_proc_getattr(dentry, &fattr);
251 if (!error) {
252 smb_renew_times(dentry);
253 /*
254 * Check whether the type part of the mode changed,
255 * and don't update the attributes if it did.
256 *
257 * And don't dick with the root inode
258 */
259 if (inode->i_ino == 2)
260 return error;
261 if (S_ISLNK(inode->i_mode))
262 return error; /* VFS will deal with it */
263
264 if ((inode->i_mode & S_IFMT) == (fattr.f_mode & S_IFMT)) {
265 smb_set_inode_attr(inode, &fattr);
266 } else {
267 /*
268 * Big trouble! The inode has become a new object,
269 * so any operations attempted on it are invalid.
270 *
271 * To limit damage, mark the inode as bad so that
272 * subsequent lookup validations will fail.
273 */
274 PARANOIA("%s/%s changed mode, %07o to %07o\n",
275 DENTRY_PATH(dentry),
276 inode->i_mode, fattr.f_mode);
277
278 fattr.f_mode = inode->i_mode; /* save mode */
279 make_bad_inode(inode);
280 inode->i_mode = fattr.f_mode; /* restore mode */
281 /*
282 * No need to worry about unhashing the dentry: the
283 * lookup validation will see that the inode is bad.
284 * But we do want to invalidate the caches ...
285 */
286 if (!S_ISDIR(inode->i_mode))
287 invalidate_remote_inode(inode);
288 else
289 smb_invalid_dir_cache(inode);
290 error = -EIO;
291 }
292 }
293 return error;
294}
295
296/*
297 * This is called when we want to check whether the inode
298 * has changed on the server. If it has changed, we must
299 * invalidate our local caches.
300 */
301int
302smb_revalidate_inode(struct dentry *dentry)
303{
304 struct smb_sb_info *s = server_from_dentry(dentry);
305 struct inode *inode = dentry->d_inode;
306 int error = 0;
307
308 DEBUG1("smb_revalidate_inode\n");
309 lock_kernel();
310
311 /*
312 * Check whether we've recently refreshed the inode.
313 */
314 if (time_before(jiffies, SMB_I(inode)->oldmtime + SMB_MAX_AGE(s))) {
315 VERBOSE("up-to-date, ino=%ld, jiffies=%lu, oldtime=%lu\n",
316 inode->i_ino, jiffies, SMB_I(inode)->oldmtime);
317 goto out;
318 }
319
320 error = smb_refresh_inode(dentry);
321out:
322 unlock_kernel();
323 return error;
324}
325
326/*
327 * This routine is called when i_nlink == 0 and i_count goes to 0.
328 * All blocking cleanup operations need to go here to avoid races.
329 */
330static void
331smb_delete_inode(struct inode *ino)
332{
333 DEBUG1("ino=%ld\n", ino->i_ino);
334 lock_kernel();
335 if (smb_close(ino))
336 PARANOIA("could not close inode %ld\n", ino->i_ino);
337 unlock_kernel();
338 clear_inode(ino);
339}
340
341static struct option opts[] = {
342 { "version", 0, 'v' },
343 { "win95", SMB_MOUNT_WIN95, 1 },
344 { "oldattr", SMB_MOUNT_OLDATTR, 1 },
345 { "dirattr", SMB_MOUNT_DIRATTR, 1 },
346 { "case", SMB_MOUNT_CASE, 1 },
347 { "uid", 0, 'u' },
348 { "gid", 0, 'g' },
349 { "file_mode", 0, 'f' },
350 { "dir_mode", 0, 'd' },
351 { "iocharset", 0, 'i' },
352 { "codepage", 0, 'c' },
353 { "ttl", 0, 't' },
354 { NULL, 0, 0}
355};
356
357static int
358parse_options(struct smb_mount_data_kernel *mnt, char *options)
359{
360 int c;
361 unsigned long flags;
362 unsigned long value;
363 char *optarg;
364 char *optopt;
365
366 flags = 0;
367 while ( (c = smb_getopt("smbfs", &options, opts,
368 &optopt, &optarg, &flags, &value)) > 0) {
369
370 VERBOSE("'%s' -> '%s'\n", optopt, optarg ? optarg : "<none>");
371 switch (c) {
372 case 1:
373 /* got a "flag" option */
374 break;
375 case 'v':
376 if (value != SMB_MOUNT_VERSION) {
377 printk ("smbfs: Bad mount version %ld, expected %d\n",
378 value, SMB_MOUNT_VERSION);
379 return 0;
380 }
381 mnt->version = value;
382 break;
383 case 'u':
384 mnt->uid = value;
385 flags |= SMB_MOUNT_UID;
386 break;
387 case 'g':
388 mnt->gid = value;
389 flags |= SMB_MOUNT_GID;
390 break;
391 case 'f':
392 mnt->file_mode = (value & S_IRWXUGO) | S_IFREG;
393 flags |= SMB_MOUNT_FMODE;
394 break;
395 case 'd':
396 mnt->dir_mode = (value & S_IRWXUGO) | S_IFDIR;
397 flags |= SMB_MOUNT_DMODE;
398 break;
399 case 'i':
400 strlcpy(mnt->codepage.local_name, optarg,
401 SMB_NLS_MAXNAMELEN);
402 break;
403 case 'c':
404 strlcpy(mnt->codepage.remote_name, optarg,
405 SMB_NLS_MAXNAMELEN);
406 break;
407 case 't':
408 mnt->ttl = value;
409 break;
410 default:
411 printk ("smbfs: Unrecognized mount option %s\n",
412 optopt);
413 return -1;
414 }
415 }
416 mnt->flags = flags;
417 return c;
418}
419
420/*
421 * smb_show_options() is for displaying mount options in /proc/mounts.
422 * It tries to avoid showing settings that were not changed from their
423 * defaults.
424 */
425static int
426smb_show_options(struct seq_file *s, struct vfsmount *m)
427{
428 struct smb_mount_data_kernel *mnt = SMB_SB(m->mnt_sb)->mnt;
429 int i;
430
431 for (i = 0; opts[i].name != NULL; i++)
432 if (mnt->flags & opts[i].flag)
433 seq_printf(s, ",%s", opts[i].name);
434
435 if (mnt->flags & SMB_MOUNT_UID)
436 seq_printf(s, ",uid=%d", mnt->uid);
437 if (mnt->flags & SMB_MOUNT_GID)
438 seq_printf(s, ",gid=%d", mnt->gid);
439 if (mnt->mounted_uid != 0)
440 seq_printf(s, ",mounted_uid=%d", mnt->mounted_uid);
441
442 /*
443 * Defaults for file_mode and dir_mode are unknown to us; they
444 * depend on the current umask of the user doing the mount.
445 */
446 if (mnt->flags & SMB_MOUNT_FMODE)
447 seq_printf(s, ",file_mode=%04o", mnt->file_mode & S_IRWXUGO);
448 if (mnt->flags & SMB_MOUNT_DMODE)
449 seq_printf(s, ",dir_mode=%04o", mnt->dir_mode & S_IRWXUGO);
450
451 if (strcmp(mnt->codepage.local_name, CONFIG_NLS_DEFAULT))
452 seq_printf(s, ",iocharset=%s", mnt->codepage.local_name);
453 if (strcmp(mnt->codepage.remote_name, SMB_NLS_REMOTE))
454 seq_printf(s, ",codepage=%s", mnt->codepage.remote_name);
455
456 if (mnt->ttl != SMB_TTL_DEFAULT)
457 seq_printf(s, ",ttl=%d", mnt->ttl);
458
459 return 0;
460}
461
462static void
463smb_unload_nls(struct smb_sb_info *server)
464{
465 if (server->remote_nls) {
466 unload_nls(server->remote_nls);
467 server->remote_nls = NULL;
468 }
469 if (server->local_nls) {
470 unload_nls(server->local_nls);
471 server->local_nls = NULL;
472 }
473}
474
475static void
476smb_put_super(struct super_block *sb)
477{
478 struct smb_sb_info *server = SMB_SB(sb);
479
480 smb_lock_server(server);
481 server->state = CONN_INVALID;
482 smbiod_unregister_server(server);
483
484 smb_close_socket(server);
485
486 if (server->conn_pid)
487 kill_proc(server->conn_pid, SIGTERM, 1);
488
489 smb_kfree(server->ops);
490 smb_unload_nls(server);
491 sb->s_fs_info = NULL;
492 smb_unlock_server(server);
493 smb_kfree(server);
494}
495
496static int smb_fill_super(struct super_block *sb, void *raw_data, int silent)
497{
498 struct smb_sb_info *server;
499 struct smb_mount_data_kernel *mnt;
500 struct smb_mount_data *oldmnt;
501 struct inode *root_inode;
502 struct smb_fattr root;
503 int ver;
504 void *mem;
505
506 if (!raw_data)
507 goto out_no_data;
508
509 oldmnt = (struct smb_mount_data *) raw_data;
510 ver = oldmnt->version;
511 if (ver != SMB_MOUNT_OLDVERSION && cpu_to_be32(ver) != SMB_MOUNT_ASCII)
512 goto out_wrong_data;
513
514 sb->s_flags |= MS_NODIRATIME;
515 sb->s_blocksize = 1024; /* Eh... Is this correct? */
516 sb->s_blocksize_bits = 10;
517 sb->s_magic = SMB_SUPER_MAGIC;
518 sb->s_op = &smb_sops;
519 sb->s_time_gran = 100;
520
521 server = smb_kmalloc(sizeof(struct smb_sb_info), GFP_KERNEL);
522 if (!server)
523 goto out_no_server;
524 sb->s_fs_info = server;
525 memset(server, 0, sizeof(struct smb_sb_info));
526
527 server->super_block = sb;
528 server->mnt = NULL;
529 server->sock_file = NULL;
530 init_waitqueue_head(&server->conn_wq);
531 init_MUTEX(&server->sem);
532 INIT_LIST_HEAD(&server->entry);
533 INIT_LIST_HEAD(&server->xmitq);
534 INIT_LIST_HEAD(&server->recvq);
535 server->conn_error = 0;
536 server->conn_pid = 0;
537 server->state = CONN_INVALID; /* no connection yet */
538 server->generation = 0;
539
540 /* Allocate the global temp buffer and some superblock helper structs */
541 /* FIXME: move these to the smb_sb_info struct */
542 VERBOSE("alloc chunk = %d\n", sizeof(struct smb_ops) +
543 sizeof(struct smb_mount_data_kernel));
544 mem = smb_kmalloc(sizeof(struct smb_ops) +
545 sizeof(struct smb_mount_data_kernel), GFP_KERNEL);
546 if (!mem)
547 goto out_no_mem;
548
549 server->ops = mem;
550 smb_install_null_ops(server->ops);
551 server->mnt = mem + sizeof(struct smb_ops);
552
553 /* Setup NLS stuff */
554 server->remote_nls = NULL;
555 server->local_nls = NULL;
556
557 mnt = server->mnt;
558
559 memset(mnt, 0, sizeof(struct smb_mount_data_kernel));
560 strlcpy(mnt->codepage.local_name, CONFIG_NLS_DEFAULT,
561 SMB_NLS_MAXNAMELEN);
562 strlcpy(mnt->codepage.remote_name, SMB_NLS_REMOTE,
563 SMB_NLS_MAXNAMELEN);
564
565 mnt->ttl = SMB_TTL_DEFAULT;
566 if (ver == SMB_MOUNT_OLDVERSION) {
567 mnt->version = oldmnt->version;
568
569 SET_UID(mnt->uid, oldmnt->uid);
570 SET_GID(mnt->gid, oldmnt->gid);
571
572 mnt->file_mode = (oldmnt->file_mode & S_IRWXUGO) | S_IFREG;
573 mnt->dir_mode = (oldmnt->dir_mode & S_IRWXUGO) | S_IFDIR;
574
575 mnt->flags = (oldmnt->file_mode >> 9) | SMB_MOUNT_UID |
576 SMB_MOUNT_GID | SMB_MOUNT_FMODE | SMB_MOUNT_DMODE;
577 } else {
578 mnt->file_mode = S_IRWXU | S_IRGRP | S_IXGRP |
579 S_IROTH | S_IXOTH | S_IFREG;
580 mnt->dir_mode = S_IRWXU | S_IRGRP | S_IXGRP |
581 S_IROTH | S_IXOTH | S_IFDIR;
582 if (parse_options(mnt, raw_data))
583 goto out_bad_option;
584 }
585 mnt->mounted_uid = current->uid;
586 smb_setcodepage(server, &mnt->codepage);
587
588 /*
589 * Display the enabled options
590 * Note: smb_proc_getattr uses these in 2.4 (but was changed in 2.2)
591 */
592 if (mnt->flags & SMB_MOUNT_OLDATTR)
593 printk("SMBFS: Using core getattr (Win 95 speedup)\n");
594 else if (mnt->flags & SMB_MOUNT_DIRATTR)
595 printk("SMBFS: Using dir ff getattr\n");
596
597 if (smbiod_register_server(server) < 0) {
598 printk(KERN_ERR "smbfs: failed to start smbiod\n");
599 goto out_no_smbiod;
600 }
601
602 /*
603 * Keep the super block locked while we get the root inode.
604 */
605 smb_init_root_dirent(server, &root, sb);
606 root_inode = smb_iget(sb, &root);
607 if (!root_inode)
608 goto out_no_root;
609
610 sb->s_root = d_alloc_root(root_inode);
611 if (!sb->s_root)
612 goto out_no_root;
613
614 smb_new_dentry(sb->s_root);
615
616 return 0;
617
618out_no_root:
619 iput(root_inode);
620out_no_smbiod:
621 smb_unload_nls(server);
622out_bad_option:
623 smb_kfree(mem);
624out_no_mem:
625 if (!server->mnt)
626 printk(KERN_ERR "smb_fill_super: allocation failure\n");
627 sb->s_fs_info = NULL;
628 smb_kfree(server);
629 goto out_fail;
630out_wrong_data:
631 printk(KERN_ERR "smbfs: mount_data version %d is not supported\n", ver);
632 goto out_fail;
633out_no_data:
634 printk(KERN_ERR "smb_fill_super: missing data argument\n");
635out_fail:
636 return -EINVAL;
637out_no_server:
638 printk(KERN_ERR "smb_fill_super: cannot allocate struct smb_sb_info\n");
639 return -ENOMEM;
640}
641
642static int
643smb_statfs(struct super_block *sb, struct kstatfs *buf)
644{
645 int result;
646
647 lock_kernel();
648
649 result = smb_proc_dskattr(sb, buf);
650
651 unlock_kernel();
652
653 buf->f_type = SMB_SUPER_MAGIC;
654 buf->f_namelen = SMB_MAXPATHLEN;
655 return result;
656}
657
658int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
659{
660 int err = smb_revalidate_inode(dentry);
661 if (!err)
662 generic_fillattr(dentry->d_inode, stat);
663 return err;
664}
665
666int
667smb_notify_change(struct dentry *dentry, struct iattr *attr)
668{
669 struct inode *inode = dentry->d_inode;
670 struct smb_sb_info *server = server_from_dentry(dentry);
671 unsigned int mask = (S_IFREG | S_IFDIR | S_IRWXUGO);
672 int error, changed, refresh = 0;
673 struct smb_fattr fattr;
674
675 lock_kernel();
676
677 error = smb_revalidate_inode(dentry);
678 if (error)
679 goto out;
680
681 if ((error = inode_change_ok(inode, attr)) < 0)
682 goto out;
683
684 error = -EPERM;
685 if ((attr->ia_valid & ATTR_UID) && (attr->ia_uid != server->mnt->uid))
686 goto out;
687
688 if ((attr->ia_valid & ATTR_GID) && (attr->ia_uid != server->mnt->gid))
689 goto out;
690
691 if ((attr->ia_valid & ATTR_MODE) && (attr->ia_mode & ~mask))
692 goto out;
693
694 if ((attr->ia_valid & ATTR_SIZE) != 0) {
695 VERBOSE("changing %s/%s, old size=%ld, new size=%ld\n",
696 DENTRY_PATH(dentry),
697 (long) inode->i_size, (long) attr->ia_size);
698
699 filemap_fdatawrite(inode->i_mapping);
700 filemap_fdatawait(inode->i_mapping);
701
702 error = smb_open(dentry, O_WRONLY);
703 if (error)
704 goto out;
705 error = server->ops->truncate(inode, attr->ia_size);
706 if (error)
707 goto out;
708 error = vmtruncate(inode, attr->ia_size);
709 if (error)
710 goto out;
711 refresh = 1;
712 }
713
714 if (server->opt.capabilities & SMB_CAP_UNIX) {
715 /* For now we don't want to set the size with setattr_unix */
716 attr->ia_valid &= ~ATTR_SIZE;
717 /* FIXME: only call if we actually want to set something? */
718 error = smb_proc_setattr_unix(dentry, attr, 0, 0);
719 if (!error)
720 refresh = 1;
721
722 goto out;
723 }
724
725 /*
726 * Initialize the fattr and check for changed fields.
727 * Note: CTIME under SMB is creation time rather than
728 * change time, so we don't attempt to change it.
729 */
730 smb_get_inode_attr(inode, &fattr);
731
732 changed = 0;
733 if ((attr->ia_valid & ATTR_MTIME) != 0) {
734 fattr.f_mtime = attr->ia_mtime;
735 changed = 1;
736 }
737 if ((attr->ia_valid & ATTR_ATIME) != 0) {
738 fattr.f_atime = attr->ia_atime;
739 /* Earlier protocols don't have an access time */
740 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2)
741 changed = 1;
742 }
743 if (changed) {
744 error = smb_proc_settime(dentry, &fattr);
745 if (error)
746 goto out;
747 refresh = 1;
748 }
749
750 /*
751 * Check for mode changes ... we're extremely limited in
752 * what can be set for SMB servers: just the read-only bit.
753 */
754 if ((attr->ia_valid & ATTR_MODE) != 0) {
755 VERBOSE("%s/%s mode change, old=%x, new=%x\n",
756 DENTRY_PATH(dentry), fattr.f_mode, attr->ia_mode);
757 changed = 0;
758 if (attr->ia_mode & S_IWUSR) {
759 if (fattr.attr & aRONLY) {
760 fattr.attr &= ~aRONLY;
761 changed = 1;
762 }
763 } else {
764 if (!(fattr.attr & aRONLY)) {
765 fattr.attr |= aRONLY;
766 changed = 1;
767 }
768 }
769 if (changed) {
770 error = smb_proc_setattr(dentry, &fattr);
771 if (error)
772 goto out;
773 refresh = 1;
774 }
775 }
776 error = 0;
777
778out:
779 if (refresh)
780 smb_refresh_inode(dentry);
781 unlock_kernel();
782 return error;
783}
784
785#ifdef DEBUG_SMB_MALLOC
786int smb_malloced;
787int smb_current_kmalloced;
788int smb_current_vmalloced;
789#endif
790
791static struct super_block *smb_get_sb(struct file_system_type *fs_type,
792 int flags, const char *dev_name, void *data)
793{
794 return get_sb_nodev(fs_type, flags, data, smb_fill_super);
795}
796
797static struct file_system_type smb_fs_type = {
798 .owner = THIS_MODULE,
799 .name = "smbfs",
800 .get_sb = smb_get_sb,
801 .kill_sb = kill_anon_super,
802 .fs_flags = FS_BINARY_MOUNTDATA,
803};
804
805static int __init init_smb_fs(void)
806{
807 int err;
808 DEBUG1("registering ...\n");
809
810#ifdef DEBUG_SMB_MALLOC
811 smb_malloced = 0;
812 smb_current_kmalloced = 0;
813 smb_current_vmalloced = 0;
814#endif
815
816 err = init_inodecache();
817 if (err)
818 goto out_inode;
819 err = smb_init_request_cache();
820 if (err)
821 goto out_request;
822 err = register_filesystem(&smb_fs_type);
823 if (err)
824 goto out;
825 return 0;
826out:
827 smb_destroy_request_cache();
828out_request:
829 destroy_inodecache();
830out_inode:
831 return err;
832}
833
834static void __exit exit_smb_fs(void)
835{
836 DEBUG1("unregistering ...\n");
837 unregister_filesystem(&smb_fs_type);
838 smb_destroy_request_cache();
839 destroy_inodecache();
840#ifdef DEBUG_SMB_MALLOC
841 printk(KERN_DEBUG "smb_malloced: %d\n", smb_malloced);
842 printk(KERN_DEBUG "smb_current_kmalloced: %d\n",smb_current_kmalloced);
843 printk(KERN_DEBUG "smb_current_vmalloced: %d\n",smb_current_vmalloced);
844#endif
845}
846
847module_init(init_smb_fs)
848module_exit(exit_smb_fs)
849MODULE_LICENSE("GPL");
diff --git a/fs/smbfs/ioctl.c b/fs/smbfs/ioctl.c
new file mode 100644
index 000000000000..dbae1f8ea26f
--- /dev/null
+++ b/fs/smbfs/ioctl.c
@@ -0,0 +1,67 @@
1/*
2 * ioctl.c
3 *
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/errno.h>
11#include <linux/fs.h>
12#include <linux/ioctl.h>
13#include <linux/time.h>
14#include <linux/mm.h>
15#include <linux/highuid.h>
16#include <linux/net.h>
17
18#include <linux/smb_fs.h>
19#include <linux/smb_mount.h>
20
21#include <asm/uaccess.h>
22
23#include "proto.h"
24
25int
26smb_ioctl(struct inode *inode, struct file *filp,
27 unsigned int cmd, unsigned long arg)
28{
29 struct smb_sb_info *server = server_from_inode(inode);
30 struct smb_conn_opt opt;
31 int result = -EINVAL;
32
33 switch (cmd) {
34 uid16_t uid16;
35 uid_t uid32;
36 case SMB_IOC_GETMOUNTUID:
37 SET_UID(uid16, server->mnt->mounted_uid);
38 result = put_user(uid16, (uid16_t __user *) arg);
39 break;
40 case SMB_IOC_GETMOUNTUID32:
41 SET_UID(uid32, server->mnt->mounted_uid);
42 result = put_user(uid32, (uid_t __user *) arg);
43 break;
44
45 case SMB_IOC_NEWCONN:
46 /* arg is smb_conn_opt, or NULL if no connection was made */
47 if (!arg) {
48 result = 0;
49 smb_lock_server(server);
50 server->state = CONN_RETRIED;
51 printk(KERN_ERR "Connection attempt failed! [%d]\n",
52 server->conn_error);
53 smbiod_flush(server);
54 smb_unlock_server(server);
55 break;
56 }
57
58 result = -EFAULT;
59 if (!copy_from_user(&opt, (void __user *)arg, sizeof(opt)))
60 result = smb_newconn(server, &opt);
61 break;
62 default:
63 break;
64 }
65
66 return result;
67}
diff --git a/fs/smbfs/proc.c b/fs/smbfs/proc.c
new file mode 100644
index 000000000000..220babe91efd
--- /dev/null
+++ b/fs/smbfs/proc.c
@@ -0,0 +1,3509 @@
1/*
2 * proc.c
3 *
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/types.h>
11#include <linux/errno.h>
12#include <linux/slab.h>
13#include <linux/fs.h>
14#include <linux/file.h>
15#include <linux/stat.h>
16#include <linux/fcntl.h>
17#include <linux/dcache.h>
18#include <linux/dirent.h>
19#include <linux/nls.h>
20#include <linux/smp_lock.h>
21#include <linux/net.h>
22#include <linux/vfs.h>
23#include <linux/smb_fs.h>
24#include <linux/smbno.h>
25#include <linux/smb_mount.h>
26
27#include <net/sock.h>
28
29#include <asm/string.h>
30#include <asm/div64.h>
31
32#include "smb_debug.h"
33#include "proto.h"
34#include "request.h"
35
36
37/* Features. Undefine if they cause problems, this should perhaps be a
38 config option. */
39#define SMBFS_POSIX_UNLINK 1
40
41/* Allow smb_retry to be interrupted. */
42#define SMB_RETRY_INTR
43
44#define SMB_VWV(packet) ((packet) + SMB_HEADER_LEN)
45#define SMB_CMD(packet) (*(packet+8))
46#define SMB_WCT(packet) (*(packet+SMB_HEADER_LEN - 1))
47
48#define SMB_DIRINFO_SIZE 43
49#define SMB_STATUS_SIZE 21
50
51#define SMB_ST_BLKSIZE (PAGE_SIZE)
52#define SMB_ST_BLKSHIFT (PAGE_SHIFT)
53
54static struct smb_ops smb_ops_core;
55static struct smb_ops smb_ops_os2;
56static struct smb_ops smb_ops_win95;
57static struct smb_ops smb_ops_winNT;
58static struct smb_ops smb_ops_unix;
59static struct smb_ops smb_ops_null;
60
61static void
62smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
63static void
64smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr);
65static int
66smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
67 struct smb_fattr *fattr);
68static int
69smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
70 struct smb_fattr *fattr);
71static int
72smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
73 u16 attr);
74static int
75smb_proc_setattr_ext(struct smb_sb_info *server,
76 struct inode *inode, struct smb_fattr *fattr);
77static int
78smb_proc_query_cifsunix(struct smb_sb_info *server);
79static void
80install_ops(struct smb_ops *dst, struct smb_ops *src);
81
82
83static void
84str_upper(char *name, int len)
85{
86 while (len--)
87 {
88 if (*name >= 'a' && *name <= 'z')
89 *name -= ('a' - 'A');
90 name++;
91 }
92}
93
94#if 0
95static void
96str_lower(char *name, int len)
97{
98 while (len--)
99 {
100 if (*name >= 'A' && *name <= 'Z')
101 *name += ('a' - 'A');
102 name++;
103 }
104}
105#endif
106
107/* reverse a string inline. This is used by the dircache walking routines */
108static void reverse_string(char *buf, int len)
109{
110 char c;
111 char *end = buf+len-1;
112
113 while(buf < end) {
114 c = *buf;
115 *(buf++) = *end;
116 *(end--) = c;
117 }
118}
119
120/* no conversion, just a wrapper for memcpy. */
121static int convert_memcpy(unsigned char *output, int olen,
122 const unsigned char *input, int ilen,
123 struct nls_table *nls_from,
124 struct nls_table *nls_to)
125{
126 if (olen < ilen)
127 return -ENAMETOOLONG;
128 memcpy(output, input, ilen);
129 return ilen;
130}
131
132static inline int write_char(unsigned char ch, char *output, int olen)
133{
134 if (olen < 4)
135 return -ENAMETOOLONG;
136 sprintf(output, ":x%02x", ch);
137 return 4;
138}
139
140static inline int write_unichar(wchar_t ch, char *output, int olen)
141{
142 if (olen < 5)
143 return -ENAMETOOLONG;
144 sprintf(output, ":%04x", ch);
145 return 5;
146}
147
148/* convert from one "codepage" to another (possibly being utf8). */
149static int convert_cp(unsigned char *output, int olen,
150 const unsigned char *input, int ilen,
151 struct nls_table *nls_from,
152 struct nls_table *nls_to)
153{
154 int len = 0;
155 int n;
156 wchar_t ch;
157
158 while (ilen > 0) {
159 /* convert by changing to unicode and back to the new cp */
160 n = nls_from->char2uni(input, ilen, &ch);
161 if (n == -EINVAL) {
162 ilen--;
163 n = write_char(*input++, output, olen);
164 if (n < 0)
165 goto fail;
166 output += n;
167 olen -= n;
168 len += n;
169 continue;
170 } else if (n < 0)
171 goto fail;
172 input += n;
173 ilen -= n;
174
175 n = nls_to->uni2char(ch, output, olen);
176 if (n == -EINVAL)
177 n = write_unichar(ch, output, olen);
178 if (n < 0)
179 goto fail;
180 output += n;
181 olen -= n;
182
183 len += n;
184 }
185 return len;
186fail:
187 return n;
188}
189
190/* ----------------------------------------------------------- */
191
192/*
193 * nls_unicode
194 *
195 * This encodes/decodes little endian unicode format
196 */
197
198static int uni2char(wchar_t uni, unsigned char *out, int boundlen)
199{
200 if (boundlen < 2)
201 return -EINVAL;
202 *out++ = uni & 0xff;
203 *out++ = uni >> 8;
204 return 2;
205}
206
207static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
208{
209 if (boundlen < 2)
210 return -EINVAL;
211 *uni = (rawstring[1] << 8) | rawstring[0];
212 return 2;
213}
214
215static struct nls_table unicode_table = {
216 .charset = "unicode",
217 .uni2char = uni2char,
218 .char2uni = char2uni,
219};
220
221/* ----------------------------------------------------------- */
222
223static int setcodepage(struct nls_table **p, char *name)
224{
225 struct nls_table *nls;
226
227 if (!name || !*name) {
228 nls = NULL;
229 } else if ( (nls = load_nls(name)) == NULL) {
230 printk (KERN_ERR "smbfs: failed to load nls '%s'\n", name);
231 return -EINVAL;
232 }
233
234 /* if already set, unload the previous one. */
235 if (*p && *p != &unicode_table)
236 unload_nls(*p);
237 *p = nls;
238
239 return 0;
240}
241
242/* Handles all changes to codepage settings. */
243int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp)
244{
245 int n = 0;
246
247 smb_lock_server(server);
248
249 /* Don't load any nls_* at all, if no remote is requested */
250 if (!*cp->remote_name)
251 goto out;
252
253 /* local */
254 n = setcodepage(&server->local_nls, cp->local_name);
255 if (n != 0)
256 goto out;
257
258 /* remote */
259 if (!strcmp(cp->remote_name, "unicode")) {
260 server->remote_nls = &unicode_table;
261 } else {
262 n = setcodepage(&server->remote_nls, cp->remote_name);
263 if (n != 0)
264 setcodepage(&server->local_nls, NULL);
265 }
266
267out:
268 if (server->local_nls != NULL && server->remote_nls != NULL)
269 server->ops->convert = convert_cp;
270 else
271 server->ops->convert = convert_memcpy;
272
273 smb_unlock_server(server);
274 return n;
275}
276
277
278/*****************************************************************************/
279/* */
280/* Encoding/Decoding section */
281/* */
282/*****************************************************************************/
283
284static __u8 *
285smb_encode_smb_length(__u8 * p, __u32 len)
286{
287 *p = 0;
288 *(p+1) = 0;
289 *(p+2) = (len & 0xFF00) >> 8;
290 *(p+3) = (len & 0xFF);
291 if (len > 0xFFFF)
292 {
293 *(p+1) = 1;
294 }
295 return p + 4;
296}
297
298/*
299 * smb_build_path: build the path to entry and name storing it in buf.
300 * The path returned will have the trailing '\0'.
301 */
302static int smb_build_path(struct smb_sb_info *server, unsigned char *buf,
303 int maxlen,
304 struct dentry *entry, struct qstr *name)
305{
306 unsigned char *path = buf;
307 int len;
308 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE) != 0;
309
310 if (maxlen < (2<<unicode))
311 return -ENAMETOOLONG;
312
313 if (maxlen > SMB_MAXPATHLEN + 1)
314 maxlen = SMB_MAXPATHLEN + 1;
315
316 if (entry == NULL)
317 goto test_name_and_out;
318
319 /*
320 * If IS_ROOT, we have to do no walking at all.
321 */
322 if (IS_ROOT(entry) && !name) {
323 *path++ = '\\';
324 if (unicode) *path++ = '\0';
325 *path++ = '\0';
326 if (unicode) *path++ = '\0';
327 return path-buf;
328 }
329
330 /*
331 * Build the path string walking the tree backward from end to ROOT
332 * and store it in reversed order [see reverse_string()]
333 */
334 dget(entry);
335 spin_lock(&entry->d_lock);
336 while (!IS_ROOT(entry)) {
337 struct dentry *parent;
338
339 if (maxlen < (3<<unicode)) {
340 spin_unlock(&entry->d_lock);
341 dput(entry);
342 return -ENAMETOOLONG;
343 }
344
345 len = server->ops->convert(path, maxlen-2,
346 entry->d_name.name, entry->d_name.len,
347 server->local_nls, server->remote_nls);
348 if (len < 0) {
349 spin_unlock(&entry->d_lock);
350 dput(entry);
351 return len;
352 }
353 reverse_string(path, len);
354 path += len;
355 if (unicode) {
356 /* Note: reverse order */
357 *path++ = '\0';
358 maxlen--;
359 }
360 *path++ = '\\';
361 maxlen -= len+1;
362
363 parent = entry->d_parent;
364 dget(parent);
365 spin_unlock(&entry->d_lock);
366 dput(entry);
367 entry = parent;
368 spin_lock(&entry->d_lock);
369 }
370 spin_unlock(&entry->d_lock);
371 dput(entry);
372 reverse_string(buf, path-buf);
373
374 /* maxlen has space for at least one char */
375test_name_and_out:
376 if (name) {
377 if (maxlen < (3<<unicode))
378 return -ENAMETOOLONG;
379 *path++ = '\\';
380 if (unicode) {
381 *path++ = '\0';
382 maxlen--;
383 }
384 len = server->ops->convert(path, maxlen-2,
385 name->name, name->len,
386 server->local_nls, server->remote_nls);
387 if (len < 0)
388 return len;
389 path += len;
390 maxlen -= len+1;
391 }
392 /* maxlen has space for at least one char */
393 *path++ = '\0';
394 if (unicode) *path++ = '\0';
395 return path-buf;
396}
397
398static int smb_encode_path(struct smb_sb_info *server, char *buf, int maxlen,
399 struct dentry *dir, struct qstr *name)
400{
401 int result;
402
403 result = smb_build_path(server, buf, maxlen, dir, name);
404 if (result < 0)
405 goto out;
406 if (server->opt.protocol <= SMB_PROTOCOL_COREPLUS)
407 str_upper(buf, result);
408out:
409 return result;
410}
411
412/* encode_path for non-trans2 request SMBs */
413static int smb_simple_encode_path(struct smb_request *req, char **p,
414 struct dentry * entry, struct qstr * name)
415{
416 struct smb_sb_info *server = req->rq_server;
417 char *s = *p;
418 int res;
419 int maxlen = ((char *)req->rq_buffer + req->rq_bufsize) - s;
420 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
421
422 if (!maxlen)
423 return -ENAMETOOLONG;
424 *s++ = 4; /* ASCII data format */
425
426 /*
427 * SMB Unicode strings must be 16bit aligned relative the start of the
428 * packet. If they are not they must be padded with 0.
429 */
430 if (unicode) {
431 int align = s - (char *)req->rq_buffer;
432 if (!(align & 1)) {
433 *s++ = '\0';
434 maxlen--;
435 }
436 }
437
438 res = smb_encode_path(server, s, maxlen-1, entry, name);
439 if (res < 0)
440 return res;
441 *p = s + res;
442 return 0;
443}
444
445/* The following are taken directly from msdos-fs */
446
447/* Linear day numbers of the respective 1sts in non-leap years. */
448
449static int day_n[] =
450{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0};
451 /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */
452
453
454static time_t
455utc2local(struct smb_sb_info *server, time_t time)
456{
457 return time - server->opt.serverzone*60;
458}
459
460static time_t
461local2utc(struct smb_sb_info *server, time_t time)
462{
463 return time + server->opt.serverzone*60;
464}
465
466/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
467
468static time_t
469date_dos2unix(struct smb_sb_info *server, __u16 date, __u16 time)
470{
471 int month, year;
472 time_t secs;
473
474 /* first subtract and mask after that... Otherwise, if
475 date == 0, bad things happen */
476 month = ((date >> 5) - 1) & 15;
477 year = date >> 9;
478 secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + 86400 *
479 ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - ((year & 3) == 0 &&
480 month < 2 ? 1 : 0) + 3653);
481 /* days since 1.1.70 plus 80's leap day */
482 return local2utc(server, secs);
483}
484
485
486/* Convert linear UNIX date to a MS-DOS time/date pair. */
487
488static void
489date_unix2dos(struct smb_sb_info *server,
490 int unix_date, __u16 *date, __u16 *time)
491{
492 int day, year, nl_day, month;
493
494 unix_date = utc2local(server, unix_date);
495 if (unix_date < 315532800)
496 unix_date = 315532800;
497
498 *time = (unix_date % 60) / 2 +
499 (((unix_date / 60) % 60) << 5) +
500 (((unix_date / 3600) % 24) << 11);
501
502 day = unix_date / 86400 - 3652;
503 year = day / 365;
504 if ((year + 3) / 4 + 365 * year > day)
505 year--;
506 day -= (year + 3) / 4 + 365 * year;
507 if (day == 59 && !(year & 3)) {
508 nl_day = day;
509 month = 2;
510 } else {
511 nl_day = (year & 3) || day <= 59 ? day : day - 1;
512 for (month = 0; month < 12; month++)
513 if (day_n[month] > nl_day)
514 break;
515 }
516 *date = nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9);
517}
518
519/* The following are taken from fs/ntfs/util.c */
520
521#define NTFS_TIME_OFFSET ((u64)(369*365 + 89) * 24 * 3600 * 10000000)
522
523/*
524 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
525 * into Unix UTC (based 1970-01-01, in seconds).
526 */
527static struct timespec
528smb_ntutc2unixutc(u64 ntutc)
529{
530 struct timespec ts;
531 /* FIXME: what about the timezone difference? */
532 /* Subtract the NTFS time offset, then convert to 1s intervals. */
533 u64 t = ntutc - NTFS_TIME_OFFSET;
534 ts.tv_nsec = do_div(t, 10000000) * 100;
535 ts.tv_sec = t;
536 return ts;
537}
538
539/* Convert the Unix UTC into NT time */
540static u64
541smb_unixutc2ntutc(struct timespec ts)
542{
543 /* Note: timezone conversion is probably wrong. */
544 /* return ((u64)utc2local(server, t)) * 10000000 + NTFS_TIME_OFFSET; */
545 return ((u64)ts.tv_sec) * 10000000 + ts.tv_nsec/100 + NTFS_TIME_OFFSET;
546}
547
548#define MAX_FILE_MODE 6
549static mode_t file_mode[] = {
550 S_IFREG, S_IFDIR, S_IFLNK, S_IFCHR, S_IFBLK, S_IFIFO, S_IFSOCK
551};
552
553static int smb_filetype_to_mode(u32 filetype)
554{
555 if (filetype > MAX_FILE_MODE) {
556 PARANOIA("Filetype out of range: %d\n", filetype);
557 return S_IFREG;
558 }
559 return file_mode[filetype];
560}
561
562static u32 smb_filetype_from_mode(int mode)
563{
564 if (S_ISREG(mode))
565 return UNIX_TYPE_FILE;
566 if (S_ISDIR(mode))
567 return UNIX_TYPE_DIR;
568 if (S_ISLNK(mode))
569 return UNIX_TYPE_SYMLINK;
570 if (S_ISCHR(mode))
571 return UNIX_TYPE_CHARDEV;
572 if (S_ISBLK(mode))
573 return UNIX_TYPE_BLKDEV;
574 if (S_ISFIFO(mode))
575 return UNIX_TYPE_FIFO;
576 if (S_ISSOCK(mode))
577 return UNIX_TYPE_SOCKET;
578 return UNIX_TYPE_UNKNOWN;
579}
580
581
582/*****************************************************************************/
583/* */
584/* Support section. */
585/* */
586/*****************************************************************************/
587
588__u32
589smb_len(__u8 * p)
590{
591 return ((*(p+1) & 0x1) << 16L) | (*(p+2) << 8L) | *(p+3);
592}
593
594static __u16
595smb_bcc(__u8 * packet)
596{
597 int pos = SMB_HEADER_LEN + SMB_WCT(packet) * sizeof(__u16);
598 return WVAL(packet, pos);
599}
600
601/* smb_valid_packet: We check if packet fulfills the basic
602 requirements of a smb packet */
603
604static int
605smb_valid_packet(__u8 * packet)
606{
607 return (packet[4] == 0xff
608 && packet[5] == 'S'
609 && packet[6] == 'M'
610 && packet[7] == 'B'
611 && (smb_len(packet) + 4 == SMB_HEADER_LEN
612 + SMB_WCT(packet) * 2 + smb_bcc(packet)));
613}
614
615/* smb_verify: We check if we got the answer we expected, and if we
616 got enough data. If bcc == -1, we don't care. */
617
618static int
619smb_verify(__u8 * packet, int command, int wct, int bcc)
620{
621 if (SMB_CMD(packet) != command)
622 goto bad_command;
623 if (SMB_WCT(packet) < wct)
624 goto bad_wct;
625 if (bcc != -1 && smb_bcc(packet) < bcc)
626 goto bad_bcc;
627 return 0;
628
629bad_command:
630 printk(KERN_ERR "smb_verify: command=%x, SMB_CMD=%x??\n",
631 command, SMB_CMD(packet));
632 goto fail;
633bad_wct:
634 printk(KERN_ERR "smb_verify: command=%x, wct=%d, SMB_WCT=%d??\n",
635 command, wct, SMB_WCT(packet));
636 goto fail;
637bad_bcc:
638 printk(KERN_ERR "smb_verify: command=%x, bcc=%d, SMB_BCC=%d??\n",
639 command, bcc, smb_bcc(packet));
640fail:
641 return -EIO;
642}
643
644/*
645 * Returns the maximum read or write size for the "payload". Making all of the
646 * packet fit within the negotiated max_xmit size.
647 *
648 * N.B. Since this value is usually computed before locking the server,
649 * the server's packet size must never be decreased!
650 */
651static inline int
652smb_get_xmitsize(struct smb_sb_info *server, int overhead)
653{
654 return server->opt.max_xmit - overhead;
655}
656
657/*
658 * Calculate the maximum read size
659 */
660int
661smb_get_rsize(struct smb_sb_info *server)
662{
663 /* readX has 12 parameters, read has 5 */
664 int overhead = SMB_HEADER_LEN + 12 * sizeof(__u16) + 2 + 1 + 2;
665 int size = smb_get_xmitsize(server, overhead);
666
667 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
668
669 return size;
670}
671
672/*
673 * Calculate the maximum write size
674 */
675int
676smb_get_wsize(struct smb_sb_info *server)
677{
678 /* writeX has 14 parameters, write has 5 */
679 int overhead = SMB_HEADER_LEN + 14 * sizeof(__u16) + 2 + 1 + 2;
680 int size = smb_get_xmitsize(server, overhead);
681
682 VERBOSE("xmit=%d, size=%d\n", server->opt.max_xmit, size);
683
684 return size;
685}
686
687/*
688 * Convert SMB error codes to -E... errno values.
689 */
690int
691smb_errno(struct smb_request *req)
692{
693 int errcls = req->rq_rcls;
694 int error = req->rq_err;
695 char *class = "Unknown";
696
697 VERBOSE("errcls %d code %d from command 0x%x\n",
698 errcls, error, SMB_CMD(req->rq_header));
699
700 if (errcls == ERRDOS) {
701 switch (error) {
702 case ERRbadfunc:
703 return -EINVAL;
704 case ERRbadfile:
705 case ERRbadpath:
706 return -ENOENT;
707 case ERRnofids:
708 return -EMFILE;
709 case ERRnoaccess:
710 return -EACCES;
711 case ERRbadfid:
712 return -EBADF;
713 case ERRbadmcb:
714 return -EREMOTEIO;
715 case ERRnomem:
716 return -ENOMEM;
717 case ERRbadmem:
718 return -EFAULT;
719 case ERRbadenv:
720 case ERRbadformat:
721 return -EREMOTEIO;
722 case ERRbadaccess:
723 return -EACCES;
724 case ERRbaddata:
725 return -E2BIG;
726 case ERRbaddrive:
727 return -ENXIO;
728 case ERRremcd:
729 return -EREMOTEIO;
730 case ERRdiffdevice:
731 return -EXDEV;
732 case ERRnofiles:
733 return -ENOENT;
734 case ERRbadshare:
735 return -ETXTBSY;
736 case ERRlock:
737 return -EDEADLK;
738 case ERRfilexists:
739 return -EEXIST;
740 case ERROR_INVALID_PARAMETER:
741 return -EINVAL;
742 case ERROR_DISK_FULL:
743 return -ENOSPC;
744 case ERROR_INVALID_NAME:
745 return -ENOENT;
746 case ERROR_DIR_NOT_EMPTY:
747 return -ENOTEMPTY;
748 case ERROR_NOT_LOCKED:
749 return -ENOLCK;
750 case ERROR_ALREADY_EXISTS:
751 return -EEXIST;
752 default:
753 class = "ERRDOS";
754 goto err_unknown;
755 }
756 } else if (errcls == ERRSRV) {
757 switch (error) {
758 /* N.B. This is wrong ... EIO ? */
759 case ERRerror:
760 return -ENFILE;
761 case ERRbadpw:
762 return -EINVAL;
763 case ERRbadtype:
764 case ERRtimeout:
765 return -EIO;
766 case ERRaccess:
767 return -EACCES;
768 /*
769 * This is a fatal error, as it means the "tree ID"
770 * for this connection is no longer valid. We map
771 * to a special error code and get a new connection.
772 */
773 case ERRinvnid:
774 return -EBADSLT;
775 default:
776 class = "ERRSRV";
777 goto err_unknown;
778 }
779 } else if (errcls == ERRHRD) {
780 switch (error) {
781 case ERRnowrite:
782 return -EROFS;
783 case ERRbadunit:
784 return -ENODEV;
785 case ERRnotready:
786 return -EUCLEAN;
787 case ERRbadcmd:
788 case ERRdata:
789 return -EIO;
790 case ERRbadreq:
791 return -ERANGE;
792 case ERRbadshare:
793 return -ETXTBSY;
794 case ERRlock:
795 return -EDEADLK;
796 case ERRdiskfull:
797 return -ENOSPC;
798 default:
799 class = "ERRHRD";
800 goto err_unknown;
801 }
802 } else if (errcls == ERRCMD) {
803 class = "ERRCMD";
804 } else if (errcls == SUCCESS) {
805 return 0; /* This is the only valid 0 return */
806 }
807
808err_unknown:
809 printk(KERN_ERR "smb_errno: class %s, code %d from command 0x%x\n",
810 class, error, SMB_CMD(req->rq_header));
811 return -EIO;
812}
813
814/* smb_request_ok: We expect the server to be locked. Then we do the
815 request and check the answer completely. When smb_request_ok
816 returns 0, you can be quite sure that everything went well. When
817 the answer is <=0, the returned number is a valid unix errno. */
818
819static int
820smb_request_ok(struct smb_request *req, int command, int wct, int bcc)
821{
822 int result;
823
824 req->rq_resp_wct = wct;
825 req->rq_resp_bcc = bcc;
826
827 result = smb_add_request(req);
828 if (result != 0) {
829 DEBUG1("smb_request failed\n");
830 goto out;
831 }
832
833 if (smb_valid_packet(req->rq_header) != 0) {
834 PARANOIA("invalid packet!\n");
835 goto out;
836 }
837
838 result = smb_verify(req->rq_header, command, wct, bcc);
839
840out:
841 return result;
842}
843
844/*
845 * This implements the NEWCONN ioctl. It installs the server pid,
846 * sets server->state to CONN_VALID, and wakes up the waiting process.
847 */
848int
849smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt)
850{
851 struct file *filp;
852 struct sock *sk;
853 int error;
854
855 VERBOSE("fd=%d, pid=%d\n", opt->fd, current->pid);
856
857 smb_lock_server(server);
858
859 /*
860 * Make sure we don't already have a valid connection ...
861 */
862 error = -EINVAL;
863 if (server->state == CONN_VALID)
864 goto out;
865
866 error = -EACCES;
867 if (current->uid != server->mnt->mounted_uid &&
868 !capable(CAP_SYS_ADMIN))
869 goto out;
870
871 error = -EBADF;
872 filp = fget(opt->fd);
873 if (!filp)
874 goto out;
875 if (!smb_valid_socket(filp->f_dentry->d_inode))
876 goto out_putf;
877
878 server->sock_file = filp;
879 server->conn_pid = current->pid;
880 server->opt = *opt;
881 server->generation += 1;
882 server->state = CONN_VALID;
883 error = 0;
884
885 if (server->conn_error) {
886 /*
887 * conn_error is the returncode we originally decided to
888 * drop the old connection on. This message should be positive
889 * and not make people ask questions on why smbfs is printing
890 * error messages ...
891 */
892 printk(KERN_INFO "SMB connection re-established (%d)\n",
893 server->conn_error);
894 server->conn_error = 0;
895 }
896
897 /*
898 * Store the server in sock user_data (Only used by sunrpc)
899 */
900 sk = SOCKET_I(filp->f_dentry->d_inode)->sk;
901 sk->sk_user_data = server;
902
903 /* chain into the data_ready callback */
904 server->data_ready = xchg(&sk->sk_data_ready, smb_data_ready);
905
906 /* check if we have an old smbmount that uses seconds for the
907 serverzone */
908 if (server->opt.serverzone > 12*60 || server->opt.serverzone < -12*60)
909 server->opt.serverzone /= 60;
910
911 /* now that we have an established connection we can detect the server
912 type and enable bug workarounds */
913 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2)
914 install_ops(server->ops, &smb_ops_core);
915 else if (server->opt.protocol == SMB_PROTOCOL_LANMAN2)
916 install_ops(server->ops, &smb_ops_os2);
917 else if (server->opt.protocol == SMB_PROTOCOL_NT1 &&
918 (server->opt.max_xmit < 0x1000) &&
919 !(server->opt.capabilities & SMB_CAP_NT_SMBS)) {
920 /* FIXME: can we kill the WIN95 flag now? */
921 server->mnt->flags |= SMB_MOUNT_WIN95;
922 VERBOSE("detected WIN95 server\n");
923 install_ops(server->ops, &smb_ops_win95);
924 } else {
925 /*
926 * Samba has max_xmit 65535
927 * NT4spX has max_xmit 4536 (or something like that)
928 * win2k has ...
929 */
930 VERBOSE("detected NT1 (Samba, NT4/5) server\n");
931 install_ops(server->ops, &smb_ops_winNT);
932 }
933
934 /* FIXME: the win9x code wants to modify these ... (seek/trunc bug) */
935 if (server->mnt->flags & SMB_MOUNT_OLDATTR) {
936 server->ops->getattr = smb_proc_getattr_core;
937 } else if (server->mnt->flags & SMB_MOUNT_DIRATTR) {
938 server->ops->getattr = smb_proc_getattr_ff;
939 }
940
941 /* Decode server capabilities */
942 if (server->opt.capabilities & SMB_CAP_LARGE_FILES) {
943 /* Should be ok to set this now, as no one can access the
944 mount until the connection has been established. */
945 SB_of(server)->s_maxbytes = ~0ULL >> 1;
946 VERBOSE("LFS enabled\n");
947 }
948 if (server->opt.capabilities & SMB_CAP_UNICODE) {
949 server->mnt->flags |= SMB_MOUNT_UNICODE;
950 VERBOSE("Unicode enabled\n");
951 } else {
952 server->mnt->flags &= ~SMB_MOUNT_UNICODE;
953 }
954#if 0
955 /* flags we may test for other patches ... */
956 if (server->opt.capabilities & SMB_CAP_LARGE_READX) {
957 VERBOSE("Large reads enabled\n");
958 }
959 if (server->opt.capabilities & SMB_CAP_LARGE_WRITEX) {
960 VERBOSE("Large writes enabled\n");
961 }
962#endif
963 if (server->opt.capabilities & SMB_CAP_UNIX) {
964 struct inode *inode;
965 VERBOSE("Using UNIX CIFS extensions\n");
966 install_ops(server->ops, &smb_ops_unix);
967 inode = SB_of(server)->s_root->d_inode;
968 if (inode)
969 inode->i_op = &smb_dir_inode_operations_unix;
970 }
971
972 VERBOSE("protocol=%d, max_xmit=%d, pid=%d capabilities=0x%x\n",
973 server->opt.protocol, server->opt.max_xmit, server->conn_pid,
974 server->opt.capabilities);
975
976 /* FIXME: this really should be done by smbmount. */
977 if (server->opt.max_xmit > SMB_MAX_PACKET_SIZE) {
978 server->opt.max_xmit = SMB_MAX_PACKET_SIZE;
979 }
980
981 smb_unlock_server(server);
982 smbiod_wake_up();
983 if (server->opt.capabilities & SMB_CAP_UNIX)
984 smb_proc_query_cifsunix(server);
985
986 server->conn_complete++;
987 wake_up_interruptible_all(&server->conn_wq);
988 return error;
989
990out:
991 smb_unlock_server(server);
992 smbiod_wake_up();
993 return error;
994
995out_putf:
996 fput(filp);
997 goto out;
998}
999
1000/* smb_setup_header: We completely set up the packet. You only have to
1001 insert the command-specific fields */
1002
1003__u8 *
1004smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc)
1005{
1006 __u32 xmit_len = SMB_HEADER_LEN + wct * sizeof(__u16) + bcc + 2;
1007 __u8 *p = req->rq_header;
1008 struct smb_sb_info *server = req->rq_server;
1009
1010 p = smb_encode_smb_length(p, xmit_len - 4);
1011
1012 *p++ = 0xff;
1013 *p++ = 'S';
1014 *p++ = 'M';
1015 *p++ = 'B';
1016 *p++ = command;
1017
1018 memset(p, '\0', 19);
1019 p += 19;
1020 p += 8;
1021
1022 if (server->opt.protocol > SMB_PROTOCOL_CORE) {
1023 int flags = SMB_FLAGS_CASELESS_PATHNAMES;
1024 int flags2 = SMB_FLAGS2_LONG_PATH_COMPONENTS |
1025 SMB_FLAGS2_EXTENDED_ATTRIBUTES; /* EA? not really ... */
1026
1027 *(req->rq_header + smb_flg) = flags;
1028 if (server->mnt->flags & SMB_MOUNT_UNICODE)
1029 flags2 |= SMB_FLAGS2_UNICODE_STRINGS;
1030 WSET(req->rq_header, smb_flg2, flags2);
1031 }
1032 *p++ = wct; /* wct */
1033 p += 2 * wct;
1034 WSET(p, 0, bcc);
1035
1036 /* Include the header in the data to send */
1037 req->rq_iovlen = 1;
1038 req->rq_iov[0].iov_base = req->rq_header;
1039 req->rq_iov[0].iov_len = xmit_len - bcc;
1040
1041 return req->rq_buffer;
1042}
1043
1044static void
1045smb_setup_bcc(struct smb_request *req, __u8 *p)
1046{
1047 u16 bcc = p - req->rq_buffer;
1048 u8 *pbcc = req->rq_header + SMB_HEADER_LEN + 2*SMB_WCT(req->rq_header);
1049
1050 WSET(pbcc, 0, bcc);
1051
1052 smb_encode_smb_length(req->rq_header, SMB_HEADER_LEN +
1053 2*SMB_WCT(req->rq_header) - 2 + bcc);
1054
1055 /* Include the "bytes" in the data to send */
1056 req->rq_iovlen = 2;
1057 req->rq_iov[1].iov_base = req->rq_buffer;
1058 req->rq_iov[1].iov_len = bcc;
1059}
1060
1061static int
1062smb_proc_seek(struct smb_sb_info *server, __u16 fileid,
1063 __u16 mode, off_t offset)
1064{
1065 int result;
1066 struct smb_request *req;
1067
1068 result = -ENOMEM;
1069 if (! (req = smb_alloc_request(server, 0)))
1070 goto out;
1071
1072 smb_setup_header(req, SMBlseek, 4, 0);
1073 WSET(req->rq_header, smb_vwv0, fileid);
1074 WSET(req->rq_header, smb_vwv1, mode);
1075 DSET(req->rq_header, smb_vwv2, offset);
1076 req->rq_flags |= SMB_REQ_NORETRY;
1077
1078 result = smb_request_ok(req, SMBlseek, 2, 0);
1079 if (result < 0) {
1080 result = 0;
1081 goto out_free;
1082 }
1083
1084 result = DVAL(req->rq_header, smb_vwv0);
1085out_free:
1086 smb_rput(req);
1087out:
1088 return result;
1089}
1090
1091static int
1092smb_proc_open(struct smb_sb_info *server, struct dentry *dentry, int wish)
1093{
1094 struct inode *ino = dentry->d_inode;
1095 struct smb_inode_info *ei = SMB_I(ino);
1096 int mode, read_write = 0x42, read_only = 0x40;
1097 int res;
1098 char *p;
1099 struct smb_request *req;
1100
1101 /*
1102 * Attempt to open r/w, unless there are no write privileges.
1103 */
1104 mode = read_write;
1105 if (!(ino->i_mode & (S_IWUSR | S_IWGRP | S_IWOTH)))
1106 mode = read_only;
1107#if 0
1108 /* FIXME: why is this code not in? below we fix it so that a caller
1109 wanting RO doesn't get RW. smb_revalidate_inode does some
1110 optimization based on access mode. tail -f needs it to be correct.
1111
1112 We must open rw since we don't do the open if called a second time
1113 with different 'wish'. Is that not supported by smb servers? */
1114 if (!(wish & (O_WRONLY | O_RDWR)))
1115 mode = read_only;
1116#endif
1117
1118 res = -ENOMEM;
1119 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1120 goto out;
1121
1122 retry:
1123 p = smb_setup_header(req, SMBopen, 2, 0);
1124 WSET(req->rq_header, smb_vwv0, mode);
1125 WSET(req->rq_header, smb_vwv1, aSYSTEM | aHIDDEN | aDIR);
1126 res = smb_simple_encode_path(req, &p, dentry, NULL);
1127 if (res < 0)
1128 goto out_free;
1129 smb_setup_bcc(req, p);
1130
1131 res = smb_request_ok(req, SMBopen, 7, 0);
1132 if (res != 0) {
1133 if (mode == read_write &&
1134 (res == -EACCES || res == -ETXTBSY || res == -EROFS))
1135 {
1136 VERBOSE("%s/%s R/W failed, error=%d, retrying R/O\n",
1137 DENTRY_PATH(dentry), res);
1138 mode = read_only;
1139 req->rq_flags = 0;
1140 goto retry;
1141 }
1142 goto out_free;
1143 }
1144 /* We should now have data in vwv[0..6]. */
1145
1146 ei->fileid = WVAL(req->rq_header, smb_vwv0);
1147 ei->attr = WVAL(req->rq_header, smb_vwv1);
1148 /* smb_vwv2 has mtime */
1149 /* smb_vwv4 has size */
1150 ei->access = (WVAL(req->rq_header, smb_vwv6) & SMB_ACCMASK);
1151 ei->open = server->generation;
1152
1153out_free:
1154 smb_rput(req);
1155out:
1156 return res;
1157}
1158
1159/*
1160 * Make sure the file is open, and check that the access
1161 * is compatible with the desired access.
1162 */
1163int
1164smb_open(struct dentry *dentry, int wish)
1165{
1166 struct inode *inode = dentry->d_inode;
1167 int result;
1168 __u16 access;
1169
1170 result = -ENOENT;
1171 if (!inode) {
1172 printk(KERN_ERR "smb_open: no inode for dentry %s/%s\n",
1173 DENTRY_PATH(dentry));
1174 goto out;
1175 }
1176
1177 if (!smb_is_open(inode)) {
1178 struct smb_sb_info *server = server_from_inode(inode);
1179 result = 0;
1180 if (!smb_is_open(inode))
1181 result = smb_proc_open(server, dentry, wish);
1182 if (result)
1183 goto out;
1184 /*
1185 * A successful open means the path is still valid ...
1186 */
1187 smb_renew_times(dentry);
1188 }
1189
1190 /*
1191 * Check whether the access is compatible with the desired mode.
1192 */
1193 result = 0;
1194 access = SMB_I(inode)->access;
1195 if (access != wish && access != SMB_O_RDWR) {
1196 PARANOIA("%s/%s access denied, access=%x, wish=%x\n",
1197 DENTRY_PATH(dentry), access, wish);
1198 result = -EACCES;
1199 }
1200out:
1201 return result;
1202}
1203
1204static int
1205smb_proc_close(struct smb_sb_info *server, __u16 fileid, __u32 mtime)
1206{
1207 struct smb_request *req;
1208 int result = -ENOMEM;
1209
1210 if (! (req = smb_alloc_request(server, 0)))
1211 goto out;
1212
1213 smb_setup_header(req, SMBclose, 3, 0);
1214 WSET(req->rq_header, smb_vwv0, fileid);
1215 DSET(req->rq_header, smb_vwv1, utc2local(server, mtime));
1216 req->rq_flags |= SMB_REQ_NORETRY;
1217 result = smb_request_ok(req, SMBclose, 0, 0);
1218
1219 smb_rput(req);
1220out:
1221 return result;
1222}
1223
1224/*
1225 * Win NT 4.0 has an apparent bug in that it fails to update the
1226 * modify time when writing to a file. As a workaround, we update
1227 * both modify and access time locally, and post the times to the
1228 * server when closing the file.
1229 */
1230static int
1231smb_proc_close_inode(struct smb_sb_info *server, struct inode * ino)
1232{
1233 struct smb_inode_info *ei = SMB_I(ino);
1234 int result = 0;
1235 if (smb_is_open(ino))
1236 {
1237 /*
1238 * We clear the open flag in advance, in case another
1239 * process observes the value while we block below.
1240 */
1241 ei->open = 0;
1242
1243 /*
1244 * Kludge alert: SMB timestamps are accurate only to
1245 * two seconds ... round the times to avoid needless
1246 * cache invalidations!
1247 */
1248 if (ino->i_mtime.tv_sec & 1) {
1249 ino->i_mtime.tv_sec--;
1250 ino->i_mtime.tv_nsec = 0;
1251 }
1252 if (ino->i_atime.tv_sec & 1) {
1253 ino->i_atime.tv_sec--;
1254 ino->i_atime.tv_nsec = 0;
1255 }
1256 /*
1257 * If the file is open with write permissions,
1258 * update the time stamps to sync mtime and atime.
1259 */
1260 if ((server->opt.capabilities & SMB_CAP_UNIX) == 0 &&
1261 (server->opt.protocol >= SMB_PROTOCOL_LANMAN2) &&
1262 !(ei->access == SMB_O_RDONLY))
1263 {
1264 struct smb_fattr fattr;
1265 smb_get_inode_attr(ino, &fattr);
1266 smb_proc_setattr_ext(server, ino, &fattr);
1267 }
1268
1269 result = smb_proc_close(server, ei->fileid, ino->i_mtime.tv_sec);
1270 /*
1271 * Force a revalidation after closing ... some servers
1272 * don't post the size until the file has been closed.
1273 */
1274 if (server->opt.protocol < SMB_PROTOCOL_NT1)
1275 ei->oldmtime = 0;
1276 ei->closed = jiffies;
1277 }
1278 return result;
1279}
1280
1281int
1282smb_close(struct inode *ino)
1283{
1284 int result = 0;
1285
1286 if (smb_is_open(ino)) {
1287 struct smb_sb_info *server = server_from_inode(ino);
1288 result = smb_proc_close_inode(server, ino);
1289 }
1290 return result;
1291}
1292
1293/*
1294 * This is used to close a file following a failed instantiate.
1295 * Since we don't have an inode, we can't use any of the above.
1296 */
1297int
1298smb_close_fileid(struct dentry *dentry, __u16 fileid)
1299{
1300 struct smb_sb_info *server = server_from_dentry(dentry);
1301 int result;
1302
1303 result = smb_proc_close(server, fileid, get_seconds());
1304 return result;
1305}
1306
1307/* In smb_proc_read and smb_proc_write we do not retry, because the
1308 file-id would not be valid after a reconnection. */
1309
1310static void
1311smb_proc_read_data(struct smb_request *req)
1312{
1313 req->rq_iov[0].iov_base = req->rq_buffer;
1314 req->rq_iov[0].iov_len = 3;
1315
1316 req->rq_iov[1].iov_base = req->rq_page;
1317 req->rq_iov[1].iov_len = req->rq_rsize;
1318 req->rq_iovlen = 2;
1319
1320 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1321}
1322
1323static int
1324smb_proc_read(struct inode *inode, loff_t offset, int count, char *data)
1325{
1326 struct smb_sb_info *server = server_from_inode(inode);
1327 __u16 returned_count, data_len;
1328 unsigned char *buf;
1329 int result;
1330 struct smb_request *req;
1331 u8 rbuf[4];
1332
1333 result = -ENOMEM;
1334 if (! (req = smb_alloc_request(server, 0)))
1335 goto out;
1336
1337 smb_setup_header(req, SMBread, 5, 0);
1338 buf = req->rq_header;
1339 WSET(buf, smb_vwv0, SMB_I(inode)->fileid);
1340 WSET(buf, smb_vwv1, count);
1341 DSET(buf, smb_vwv2, offset);
1342 WSET(buf, smb_vwv4, 0);
1343
1344 req->rq_page = data;
1345 req->rq_rsize = count;
1346 req->rq_callback = smb_proc_read_data;
1347 req->rq_buffer = rbuf;
1348 req->rq_flags |= SMB_REQ_NORETRY | SMB_REQ_STATIC;
1349
1350 result = smb_request_ok(req, SMBread, 5, -1);
1351 if (result < 0)
1352 goto out_free;
1353 returned_count = WVAL(req->rq_header, smb_vwv0);
1354
1355 data_len = WVAL(rbuf, 1);
1356
1357 if (returned_count != data_len) {
1358 printk(KERN_NOTICE "smb_proc_read: returned != data_len\n");
1359 printk(KERN_NOTICE "smb_proc_read: ret_c=%d, data_len=%d\n",
1360 returned_count, data_len);
1361 }
1362 result = data_len;
1363
1364out_free:
1365 smb_rput(req);
1366out:
1367 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1368 inode->i_ino, SMB_I(inode)->fileid, count, result);
1369 return result;
1370}
1371
1372static int
1373smb_proc_write(struct inode *inode, loff_t offset, int count, const char *data)
1374{
1375 struct smb_sb_info *server = server_from_inode(inode);
1376 int result;
1377 u16 fileid = SMB_I(inode)->fileid;
1378 u8 buf[4];
1379 struct smb_request *req;
1380
1381 result = -ENOMEM;
1382 if (! (req = smb_alloc_request(server, 0)))
1383 goto out;
1384
1385 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1386 inode->i_ino, fileid, count, offset);
1387
1388 smb_setup_header(req, SMBwrite, 5, count + 3);
1389 WSET(req->rq_header, smb_vwv0, fileid);
1390 WSET(req->rq_header, smb_vwv1, count);
1391 DSET(req->rq_header, smb_vwv2, offset);
1392 WSET(req->rq_header, smb_vwv4, 0);
1393
1394 buf[0] = 1;
1395 WSET(buf, 1, count); /* yes, again ... */
1396 req->rq_iov[1].iov_base = buf;
1397 req->rq_iov[1].iov_len = 3;
1398 req->rq_iov[2].iov_base = (char *) data;
1399 req->rq_iov[2].iov_len = count;
1400 req->rq_iovlen = 3;
1401 req->rq_flags |= SMB_REQ_NORETRY;
1402
1403 result = smb_request_ok(req, SMBwrite, 1, 0);
1404 if (result >= 0)
1405 result = WVAL(req->rq_header, smb_vwv0);
1406
1407 smb_rput(req);
1408out:
1409 return result;
1410}
1411
1412/*
1413 * In smb_proc_readX and smb_proc_writeX we do not retry, because the
1414 * file-id would not be valid after a reconnection.
1415 */
1416
1417#define SMB_READX_MAX_PAD 64
1418static void
1419smb_proc_readX_data(struct smb_request *req)
1420{
1421 /* header length, excluding the netbios length (-4) */
1422 int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
1423 int data_off = WVAL(req->rq_header, smb_vwv6);
1424
1425 /*
1426 * Some genius made the padding to the data bytes arbitrary.
1427 * So we must first calculate the amount of padding used by the server.
1428 */
1429 data_off -= hdrlen;
1430 if (data_off > SMB_READX_MAX_PAD || data_off < 0) {
1431 PARANOIA("offset is larger than SMB_READX_MAX_PAD or negative!\n");
1432 PARANOIA("%d > %d || %d < 0\n", data_off, SMB_READX_MAX_PAD, data_off);
1433 req->rq_rlen = req->rq_bufsize + 1;
1434 return;
1435 }
1436 req->rq_iov[0].iov_base = req->rq_buffer;
1437 req->rq_iov[0].iov_len = data_off;
1438
1439 req->rq_iov[1].iov_base = req->rq_page;
1440 req->rq_iov[1].iov_len = req->rq_rsize;
1441 req->rq_iovlen = 2;
1442
1443 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
1444}
1445
1446static int
1447smb_proc_readX(struct inode *inode, loff_t offset, int count, char *data)
1448{
1449 struct smb_sb_info *server = server_from_inode(inode);
1450 unsigned char *buf;
1451 int result;
1452 struct smb_request *req;
1453 static char pad[SMB_READX_MAX_PAD];
1454
1455 result = -ENOMEM;
1456 if (! (req = smb_alloc_request(server, 0)))
1457 goto out;
1458
1459 smb_setup_header(req, SMBreadX, 12, 0);
1460 buf = req->rq_header;
1461 WSET(buf, smb_vwv0, 0x00ff);
1462 WSET(buf, smb_vwv1, 0);
1463 WSET(buf, smb_vwv2, SMB_I(inode)->fileid);
1464 DSET(buf, smb_vwv3, (u32)offset); /* low 32 bits */
1465 WSET(buf, smb_vwv5, count);
1466 WSET(buf, smb_vwv6, 0);
1467 DSET(buf, smb_vwv7, 0);
1468 WSET(buf, smb_vwv9, 0);
1469 DSET(buf, smb_vwv10, (u32)(offset >> 32)); /* high 32 bits */
1470 WSET(buf, smb_vwv11, 0);
1471
1472 req->rq_page = data;
1473 req->rq_rsize = count;
1474 req->rq_callback = smb_proc_readX_data;
1475 req->rq_buffer = pad;
1476 req->rq_bufsize = SMB_READX_MAX_PAD;
1477 req->rq_flags |= SMB_REQ_STATIC | SMB_REQ_NORETRY;
1478
1479 result = smb_request_ok(req, SMBreadX, 12, -1);
1480 if (result < 0)
1481 goto out_free;
1482 result = WVAL(req->rq_header, smb_vwv5);
1483
1484out_free:
1485 smb_rput(req);
1486out:
1487 VERBOSE("ino=%ld, fileid=%d, count=%d, result=%d\n",
1488 inode->i_ino, SMB_I(inode)->fileid, count, result);
1489 return result;
1490}
1491
1492static int
1493smb_proc_writeX(struct inode *inode, loff_t offset, int count, const char *data)
1494{
1495 struct smb_sb_info *server = server_from_inode(inode);
1496 int result;
1497 u8 *p;
1498 static u8 pad[4];
1499 struct smb_request *req;
1500
1501 result = -ENOMEM;
1502 if (! (req = smb_alloc_request(server, 0)))
1503 goto out;
1504
1505 VERBOSE("ino=%ld, fileid=%d, count=%d@%Ld\n",
1506 inode->i_ino, SMB_I(inode)->fileid, count, offset);
1507
1508 p = smb_setup_header(req, SMBwriteX, 14, count + 1);
1509 WSET(req->rq_header, smb_vwv0, 0x00ff);
1510 WSET(req->rq_header, smb_vwv1, 0);
1511 WSET(req->rq_header, smb_vwv2, SMB_I(inode)->fileid);
1512 DSET(req->rq_header, smb_vwv3, (u32)offset); /* low 32 bits */
1513 DSET(req->rq_header, smb_vwv5, 0);
1514 WSET(req->rq_header, smb_vwv7, 0); /* write mode */
1515 WSET(req->rq_header, smb_vwv8, 0);
1516 WSET(req->rq_header, smb_vwv9, 0);
1517 WSET(req->rq_header, smb_vwv10, count); /* data length */
1518 WSET(req->rq_header, smb_vwv11, smb_vwv12 + 2 + 1);
1519 DSET(req->rq_header, smb_vwv12, (u32)(offset >> 32));
1520
1521 req->rq_iov[1].iov_base = pad;
1522 req->rq_iov[1].iov_len = 1;
1523 req->rq_iov[2].iov_base = (char *) data;
1524 req->rq_iov[2].iov_len = count;
1525 req->rq_iovlen = 3;
1526 req->rq_flags |= SMB_REQ_NORETRY;
1527
1528 result = smb_request_ok(req, SMBwriteX, 6, 0);
1529 if (result >= 0)
1530 result = WVAL(req->rq_header, smb_vwv2);
1531
1532 smb_rput(req);
1533out:
1534 return result;
1535}
1536
1537int
1538smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid)
1539{
1540 struct smb_sb_info *server = server_from_dentry(dentry);
1541 char *p;
1542 int result;
1543 struct smb_request *req;
1544
1545 result = -ENOMEM;
1546 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1547 goto out;
1548
1549 p = smb_setup_header(req, SMBcreate, 3, 0);
1550 WSET(req->rq_header, smb_vwv0, attr);
1551 DSET(req->rq_header, smb_vwv1, utc2local(server, ctime));
1552 result = smb_simple_encode_path(req, &p, dentry, NULL);
1553 if (result < 0)
1554 goto out_free;
1555 smb_setup_bcc(req, p);
1556
1557 result = smb_request_ok(req, SMBcreate, 1, 0);
1558 if (result < 0)
1559 goto out_free;
1560
1561 *fileid = WVAL(req->rq_header, smb_vwv0);
1562 result = 0;
1563
1564out_free:
1565 smb_rput(req);
1566out:
1567 return result;
1568}
1569
1570int
1571smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry)
1572{
1573 struct smb_sb_info *server = server_from_dentry(old_dentry);
1574 char *p;
1575 int result;
1576 struct smb_request *req;
1577
1578 result = -ENOMEM;
1579 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1580 goto out;
1581
1582 p = smb_setup_header(req, SMBmv, 1, 0);
1583 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN | aDIR);
1584 result = smb_simple_encode_path(req, &p, old_dentry, NULL);
1585 if (result < 0)
1586 goto out_free;
1587 result = smb_simple_encode_path(req, &p, new_dentry, NULL);
1588 if (result < 0)
1589 goto out_free;
1590 smb_setup_bcc(req, p);
1591
1592 if ((result = smb_request_ok(req, SMBmv, 0, 0)) < 0)
1593 goto out_free;
1594 result = 0;
1595
1596out_free:
1597 smb_rput(req);
1598out:
1599 return result;
1600}
1601
1602/*
1603 * Code common to mkdir and rmdir.
1604 */
1605static int
1606smb_proc_generic_command(struct dentry *dentry, __u8 command)
1607{
1608 struct smb_sb_info *server = server_from_dentry(dentry);
1609 char *p;
1610 int result;
1611 struct smb_request *req;
1612
1613 result = -ENOMEM;
1614 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1615 goto out;
1616
1617 p = smb_setup_header(req, command, 0, 0);
1618 result = smb_simple_encode_path(req, &p, dentry, NULL);
1619 if (result < 0)
1620 goto out_free;
1621 smb_setup_bcc(req, p);
1622
1623 result = smb_request_ok(req, command, 0, 0);
1624 if (result < 0)
1625 goto out_free;
1626 result = 0;
1627
1628out_free:
1629 smb_rput(req);
1630out:
1631 return result;
1632}
1633
1634int
1635smb_proc_mkdir(struct dentry *dentry)
1636{
1637 return smb_proc_generic_command(dentry, SMBmkdir);
1638}
1639
1640int
1641smb_proc_rmdir(struct dentry *dentry)
1642{
1643 return smb_proc_generic_command(dentry, SMBrmdir);
1644}
1645
1646#if SMBFS_POSIX_UNLINK
1647/*
1648 * Removes readonly attribute from a file. Used by unlink to give posix
1649 * semantics.
1650 */
1651static int
1652smb_set_rw(struct dentry *dentry,struct smb_sb_info *server)
1653{
1654 int result;
1655 struct smb_fattr fattr;
1656
1657 /* FIXME: cifsUE should allow removing a readonly file. */
1658
1659 /* first get current attribute */
1660 smb_init_dirent(server, &fattr);
1661 result = server->ops->getattr(server, dentry, &fattr);
1662 smb_finish_dirent(server, &fattr);
1663 if (result < 0)
1664 return result;
1665
1666 /* if RONLY attribute is set, remove it */
1667 if (fattr.attr & aRONLY) { /* read only attribute is set */
1668 fattr.attr &= ~aRONLY;
1669 result = smb_proc_setattr_core(server, dentry, fattr.attr);
1670 }
1671 return result;
1672}
1673#endif
1674
1675int
1676smb_proc_unlink(struct dentry *dentry)
1677{
1678 struct smb_sb_info *server = server_from_dentry(dentry);
1679 int flag = 0;
1680 char *p;
1681 int result;
1682 struct smb_request *req;
1683
1684 result = -ENOMEM;
1685 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
1686 goto out;
1687
1688 retry:
1689 p = smb_setup_header(req, SMBunlink, 1, 0);
1690 WSET(req->rq_header, smb_vwv0, aSYSTEM | aHIDDEN);
1691 result = smb_simple_encode_path(req, &p, dentry, NULL);
1692 if (result < 0)
1693 goto out_free;
1694 smb_setup_bcc(req, p);
1695
1696 if ((result = smb_request_ok(req, SMBunlink, 0, 0)) < 0) {
1697#if SMBFS_POSIX_UNLINK
1698 if (result == -EACCES && !flag) {
1699 /* Posix semantics is for the read-only state
1700 of a file to be ignored in unlink(). In the
1701 SMB world a unlink() is refused on a
1702 read-only file. To make things easier for
1703 unix users we try to override the files
1704 permission if the unlink fails with the
1705 right error.
1706 This introduces a race condition that could
1707 lead to a file being written by someone who
1708 shouldn't have access, but as far as I can
1709 tell that is unavoidable */
1710
1711 /* remove RONLY attribute and try again */
1712 result = smb_set_rw(dentry,server);
1713 if (result == 0) {
1714 flag = 1;
1715 req->rq_flags = 0;
1716 goto retry;
1717 }
1718 }
1719#endif
1720 goto out_free;
1721 }
1722 result = 0;
1723
1724out_free:
1725 smb_rput(req);
1726out:
1727 return result;
1728}
1729
1730int
1731smb_proc_flush(struct smb_sb_info *server, __u16 fileid)
1732{
1733 int result;
1734 struct smb_request *req;
1735
1736 result = -ENOMEM;
1737 if (! (req = smb_alloc_request(server, 0)))
1738 goto out;
1739
1740 smb_setup_header(req, SMBflush, 1, 0);
1741 WSET(req->rq_header, smb_vwv0, fileid);
1742 req->rq_flags |= SMB_REQ_NORETRY;
1743 result = smb_request_ok(req, SMBflush, 0, 0);
1744
1745 smb_rput(req);
1746out:
1747 return result;
1748}
1749
1750static int
1751smb_proc_trunc32(struct inode *inode, loff_t length)
1752{
1753 /*
1754 * Writing 0bytes is old-SMB magic for truncating files.
1755 * MAX_NON_LFS should prevent this from being called with a too
1756 * large offset.
1757 */
1758 return smb_proc_write(inode, length, 0, NULL);
1759}
1760
1761static int
1762smb_proc_trunc64(struct inode *inode, loff_t length)
1763{
1764 struct smb_sb_info *server = server_from_inode(inode);
1765 int result;
1766 char *param;
1767 char *data;
1768 struct smb_request *req;
1769
1770 result = -ENOMEM;
1771 if (! (req = smb_alloc_request(server, 14)))
1772 goto out;
1773
1774 param = req->rq_buffer;
1775 data = req->rq_buffer + 6;
1776
1777 /* FIXME: must we also set allocation size? winNT seems to do that */
1778 WSET(param, 0, SMB_I(inode)->fileid);
1779 WSET(param, 2, SMB_SET_FILE_END_OF_FILE_INFO);
1780 WSET(param, 4, 0);
1781 LSET(data, 0, length);
1782
1783 req->rq_trans2_command = TRANSACT2_SETFILEINFO;
1784 req->rq_ldata = 8;
1785 req->rq_data = data;
1786 req->rq_lparm = 6;
1787 req->rq_parm = param;
1788 req->rq_flags |= SMB_REQ_NORETRY;
1789 result = smb_add_request(req);
1790 if (result < 0)
1791 goto out_free;
1792
1793 result = 0;
1794 if (req->rq_rcls != 0)
1795 result = smb_errno(req);
1796
1797out_free:
1798 smb_rput(req);
1799out:
1800 return result;
1801}
1802
1803static int
1804smb_proc_trunc95(struct inode *inode, loff_t length)
1805{
1806 struct smb_sb_info *server = server_from_inode(inode);
1807 int result = smb_proc_trunc32(inode, length);
1808
1809 /*
1810 * win9x doesn't appear to update the size immediately.
1811 * It will return the old file size after the truncate,
1812 * confusing smbfs. So we force an update.
1813 *
1814 * FIXME: is this still necessary?
1815 */
1816 smb_proc_flush(server, SMB_I(inode)->fileid);
1817 return result;
1818}
1819
1820static void
1821smb_init_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1822{
1823 memset(fattr, 0, sizeof(*fattr));
1824
1825 fattr->f_nlink = 1;
1826 fattr->f_uid = server->mnt->uid;
1827 fattr->f_gid = server->mnt->gid;
1828 fattr->f_blksize = SMB_ST_BLKSIZE;
1829 fattr->f_unix = 0;
1830}
1831
1832static void
1833smb_finish_dirent(struct smb_sb_info *server, struct smb_fattr *fattr)
1834{
1835 if (fattr->f_unix)
1836 return;
1837
1838 fattr->f_mode = server->mnt->file_mode;
1839 if (fattr->attr & aDIR) {
1840 fattr->f_mode = server->mnt->dir_mode;
1841 fattr->f_size = SMB_ST_BLKSIZE;
1842 }
1843 /* Check the read-only flag */
1844 if (fattr->attr & aRONLY)
1845 fattr->f_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
1846
1847 /* How many 512 byte blocks do we need for this file? */
1848 fattr->f_blocks = 0;
1849 if (fattr->f_size != 0)
1850 fattr->f_blocks = 1 + ((fattr->f_size-1) >> 9);
1851 return;
1852}
1853
1854void
1855smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr,
1856 struct super_block *sb)
1857{
1858 smb_init_dirent(server, fattr);
1859 fattr->attr = aDIR;
1860 fattr->f_ino = 2; /* traditional root inode number */
1861 fattr->f_mtime = current_fs_time(sb);
1862 smb_finish_dirent(server, fattr);
1863}
1864
1865/*
1866 * Decode a dirent for old protocols
1867 *
1868 * qname is filled with the decoded, and possibly translated, name.
1869 * fattr receives decoded attributes
1870 *
1871 * Bugs Noted:
1872 * (1) Pathworks servers may pad the name with extra spaces.
1873 */
1874static char *
1875smb_decode_short_dirent(struct smb_sb_info *server, char *p,
1876 struct qstr *qname, struct smb_fattr *fattr,
1877 unsigned char *name_buf)
1878{
1879 int len;
1880
1881 /*
1882 * SMB doesn't have a concept of inode numbers ...
1883 */
1884 smb_init_dirent(server, fattr);
1885 fattr->f_ino = 0; /* FIXME: do we need this? */
1886
1887 p += SMB_STATUS_SIZE; /* reserved (search_status) */
1888 fattr->attr = *p;
1889 fattr->f_mtime.tv_sec = date_dos2unix(server, WVAL(p, 3), WVAL(p, 1));
1890 fattr->f_mtime.tv_nsec = 0;
1891 fattr->f_size = DVAL(p, 5);
1892 fattr->f_ctime = fattr->f_mtime;
1893 fattr->f_atime = fattr->f_mtime;
1894 qname->name = p + 9;
1895 len = strnlen(qname->name, 12);
1896
1897 /*
1898 * Trim trailing blanks for Pathworks servers
1899 */
1900 while (len > 2 && qname->name[len-1] == ' ')
1901 len--;
1902
1903 smb_finish_dirent(server, fattr);
1904
1905#if 0
1906 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
1907 allow the flag to be set anyway. It kills const. Remove? */
1908 switch (server->opt.case_handling) {
1909 case SMB_CASE_UPPER:
1910 str_upper(entry->name, len);
1911 break;
1912 case SMB_CASE_LOWER:
1913 str_lower(entry->name, len);
1914 break;
1915 default:
1916 break;
1917 }
1918#endif
1919
1920 qname->len = 0;
1921 len = server->ops->convert(name_buf, SMB_MAXNAMELEN,
1922 qname->name, len,
1923 server->remote_nls, server->local_nls);
1924 if (len > 0) {
1925 qname->len = len;
1926 qname->name = name_buf;
1927 DEBUG1("len=%d, name=%.*s\n",qname->len,qname->len,qname->name);
1928 }
1929
1930 return p + 22;
1931}
1932
1933/*
1934 * This routine is used to read in directory entries from the network.
1935 * Note that it is for short directory name seeks, i.e.: protocol <
1936 * SMB_PROTOCOL_LANMAN2
1937 */
1938static int
1939smb_proc_readdir_short(struct file *filp, void *dirent, filldir_t filldir,
1940 struct smb_cache_control *ctl)
1941{
1942 struct dentry *dir = filp->f_dentry;
1943 struct smb_sb_info *server = server_from_dentry(dir);
1944 struct qstr qname;
1945 struct smb_fattr fattr;
1946 char *p;
1947 int result;
1948 int i, first, entries_seen, entries;
1949 int entries_asked = (server->opt.max_xmit - 100) / SMB_DIRINFO_SIZE;
1950 __u16 bcc;
1951 __u16 count;
1952 char status[SMB_STATUS_SIZE];
1953 static struct qstr mask = {
1954 .name = "*.*",
1955 .len = 3,
1956 };
1957 unsigned char *last_status;
1958 struct smb_request *req;
1959 unsigned char *name_buf;
1960
1961 VERBOSE("%s/%s\n", DENTRY_PATH(dir));
1962
1963 lock_kernel();
1964
1965 result = -ENOMEM;
1966 if (! (name_buf = kmalloc(SMB_MAXNAMELEN, GFP_KERNEL)))
1967 goto out;
1968
1969 first = 1;
1970 entries = 0;
1971 entries_seen = 2; /* implicit . and .. */
1972
1973 result = -ENOMEM;
1974 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
1975 goto out_name;
1976
1977 while (1) {
1978 p = smb_setup_header(req, SMBsearch, 2, 0);
1979 WSET(req->rq_header, smb_vwv0, entries_asked);
1980 WSET(req->rq_header, smb_vwv1, aDIR);
1981 if (first == 1) {
1982 result = smb_simple_encode_path(req, &p, dir, &mask);
1983 if (result < 0)
1984 goto out_free;
1985 if (p + 3 > (char *)req->rq_buffer + req->rq_bufsize) {
1986 result = -ENAMETOOLONG;
1987 goto out_free;
1988 }
1989 *p++ = 5;
1990 WSET(p, 0, 0);
1991 p += 2;
1992 first = 0;
1993 } else {
1994 if (p + 5 + SMB_STATUS_SIZE >
1995 (char *)req->rq_buffer + req->rq_bufsize) {
1996 result = -ENAMETOOLONG;
1997 goto out_free;
1998 }
1999
2000 *p++ = 4;
2001 *p++ = 0;
2002 *p++ = 5;
2003 WSET(p, 0, SMB_STATUS_SIZE);
2004 p += 2;
2005 memcpy(p, status, SMB_STATUS_SIZE);
2006 p += SMB_STATUS_SIZE;
2007 }
2008
2009 smb_setup_bcc(req, p);
2010
2011 result = smb_request_ok(req, SMBsearch, 1, -1);
2012 if (result < 0) {
2013 if ((req->rq_rcls == ERRDOS) &&
2014 (req->rq_err == ERRnofiles))
2015 break;
2016 goto out_free;
2017 }
2018 count = WVAL(req->rq_header, smb_vwv0);
2019 if (count <= 0)
2020 break;
2021
2022 result = -EIO;
2023 bcc = smb_bcc(req->rq_header);
2024 if (bcc != count * SMB_DIRINFO_SIZE + 3)
2025 goto out_free;
2026 p = req->rq_buffer + 3;
2027
2028
2029 /* Make sure the response fits in the buffer. Fixed sized
2030 entries means we don't have to check in the decode loop. */
2031
2032 last_status = req->rq_buffer + 3 + (count-1) * SMB_DIRINFO_SIZE;
2033
2034 if (last_status + SMB_DIRINFO_SIZE >=
2035 req->rq_buffer + req->rq_bufsize) {
2036 printk(KERN_ERR "smb_proc_readdir_short: "
2037 "last dir entry outside buffer! "
2038 "%d@%p %d@%p\n", SMB_DIRINFO_SIZE, last_status,
2039 req->rq_bufsize, req->rq_buffer);
2040 goto out_free;
2041 }
2042
2043 /* Read the last entry into the status field. */
2044 memcpy(status, last_status, SMB_STATUS_SIZE);
2045
2046
2047 /* Now we are ready to parse smb directory entries. */
2048
2049 for (i = 0; i < count; i++) {
2050 p = smb_decode_short_dirent(server, p,
2051 &qname, &fattr, name_buf);
2052 if (qname.len == 0)
2053 continue;
2054
2055 if (entries_seen == 2 && qname.name[0] == '.') {
2056 if (qname.len == 1)
2057 continue;
2058 if (qname.name[1] == '.' && qname.len == 2)
2059 continue;
2060 }
2061 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2062 &qname, &fattr))
2063 ; /* stop reading? */
2064 entries_seen++;
2065 }
2066 }
2067 result = entries;
2068
2069out_free:
2070 smb_rput(req);
2071out_name:
2072 kfree(name_buf);
2073out:
2074 unlock_kernel();
2075 return result;
2076}
2077
2078static void smb_decode_unix_basic(struct smb_fattr *fattr, struct smb_sb_info *server, char *p)
2079{
2080 u64 size, disk_bytes;
2081
2082 /* FIXME: verify nls support. all is sent as utf8? */
2083
2084 fattr->f_unix = 1;
2085 fattr->f_mode = 0;
2086
2087 /* FIXME: use the uniqueID from the remote instead? */
2088 /* 0 L file size in bytes */
2089 /* 8 L file size on disk in bytes (block count) */
2090 /* 40 L uid */
2091 /* 48 L gid */
2092 /* 56 W file type */
2093 /* 60 L devmajor */
2094 /* 68 L devminor */
2095 /* 76 L unique ID (inode) */
2096 /* 84 L permissions */
2097 /* 92 L link count */
2098
2099 size = LVAL(p, 0);
2100 disk_bytes = LVAL(p, 8);
2101
2102 /*
2103 * Some samba versions round up on-disk byte usage
2104 * to 1MB boundaries, making it useless. When seeing
2105 * that, use the size instead.
2106 */
2107 if (!(disk_bytes & 0xfffff))
2108 disk_bytes = size+511;
2109
2110 fattr->f_size = size;
2111 fattr->f_blocks = disk_bytes >> 9;
2112 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 16));
2113 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 24));
2114 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 32));
2115
2116 if (server->mnt->flags & SMB_MOUNT_UID)
2117 fattr->f_uid = server->mnt->uid;
2118 else
2119 fattr->f_uid = LVAL(p, 40);
2120
2121 if (server->mnt->flags & SMB_MOUNT_GID)
2122 fattr->f_gid = server->mnt->gid;
2123 else
2124 fattr->f_gid = LVAL(p, 48);
2125
2126 fattr->f_mode |= smb_filetype_to_mode(WVAL(p, 56));
2127
2128 if (S_ISBLK(fattr->f_mode) || S_ISCHR(fattr->f_mode)) {
2129 __u64 major = LVAL(p, 60);
2130 __u64 minor = LVAL(p, 68);
2131
2132 fattr->f_rdev = MKDEV(major & 0xffffffff, minor & 0xffffffff);
2133 if (MAJOR(fattr->f_rdev) != (major & 0xffffffff) ||
2134 MINOR(fattr->f_rdev) != (minor & 0xffffffff))
2135 fattr->f_rdev = 0;
2136 }
2137
2138 fattr->f_mode |= LVAL(p, 84);
2139
2140 if ( (server->mnt->flags & SMB_MOUNT_DMODE) &&
2141 (S_ISDIR(fattr->f_mode)) )
2142 fattr->f_mode = (server->mnt->dir_mode & S_IRWXUGO) | S_IFDIR;
2143 else if ( (server->mnt->flags & SMB_MOUNT_FMODE) &&
2144 !(S_ISDIR(fattr->f_mode)) )
2145 fattr->f_mode = (server->mnt->file_mode & S_IRWXUGO) |
2146 (fattr->f_mode & S_IFMT);
2147
2148}
2149
2150/*
2151 * Interpret a long filename structure using the specified info level:
2152 * level 1 for anything below NT1 protocol
2153 * level 260 for NT1 protocol
2154 *
2155 * qname is filled with the decoded, and possibly translated, name
2156 * fattr receives decoded attributes.
2157 *
2158 * Bugs Noted:
2159 * (1) Win NT 4.0 appends a null byte to names and counts it in the length!
2160 */
2161static char *
2162smb_decode_long_dirent(struct smb_sb_info *server, char *p, int level,
2163 struct qstr *qname, struct smb_fattr *fattr,
2164 unsigned char *name_buf)
2165{
2166 char *result;
2167 unsigned int len = 0;
2168 int n;
2169 __u16 date, time;
2170 int unicode = (server->mnt->flags & SMB_MOUNT_UNICODE);
2171
2172 /*
2173 * SMB doesn't have a concept of inode numbers ...
2174 */
2175 smb_init_dirent(server, fattr);
2176 fattr->f_ino = 0; /* FIXME: do we need this? */
2177
2178 switch (level) {
2179 case 1:
2180 len = *((unsigned char *) p + 22);
2181 qname->name = p + 23;
2182 result = p + 24 + len;
2183
2184 date = WVAL(p, 0);
2185 time = WVAL(p, 2);
2186 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2187 fattr->f_ctime.tv_nsec = 0;
2188
2189 date = WVAL(p, 4);
2190 time = WVAL(p, 6);
2191 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2192 fattr->f_atime.tv_nsec = 0;
2193
2194 date = WVAL(p, 8);
2195 time = WVAL(p, 10);
2196 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2197 fattr->f_mtime.tv_nsec = 0;
2198 fattr->f_size = DVAL(p, 12);
2199 /* ULONG allocation size */
2200 fattr->attr = WVAL(p, 20);
2201
2202 VERBOSE("info 1 at %p, len=%d, name=%.*s\n",
2203 p, len, len, qname->name);
2204 break;
2205 case 260:
2206 result = p + WVAL(p, 0);
2207 len = DVAL(p, 60);
2208 if (len > 255) len = 255;
2209 /* NT4 null terminates, unless we are using unicode ... */
2210 qname->name = p + 94;
2211 if (!unicode && len && qname->name[len-1] == '\0')
2212 len--;
2213
2214 fattr->f_ctime = smb_ntutc2unixutc(LVAL(p, 8));
2215 fattr->f_atime = smb_ntutc2unixutc(LVAL(p, 16));
2216 fattr->f_mtime = smb_ntutc2unixutc(LVAL(p, 24));
2217 /* change time (32) */
2218 fattr->f_size = LVAL(p, 40);
2219 /* alloc size (48) */
2220 fattr->attr = DVAL(p, 56);
2221
2222 VERBOSE("info 260 at %p, len=%d, name=%.*s\n",
2223 p, len, len, qname->name);
2224 break;
2225 case SMB_FIND_FILE_UNIX:
2226 result = p + WVAL(p, 0);
2227 qname->name = p + 108;
2228
2229 len = strlen(qname->name);
2230 /* FIXME: should we check the length?? */
2231
2232 p += 8;
2233 smb_decode_unix_basic(fattr, server, p);
2234 VERBOSE("info SMB_FIND_FILE_UNIX at %p, len=%d, name=%.*s\n",
2235 p, len, len, qname->name);
2236 break;
2237 default:
2238 PARANOIA("Unknown info level %d\n", level);
2239 result = p + WVAL(p, 0);
2240 goto out;
2241 }
2242
2243 smb_finish_dirent(server, fattr);
2244
2245#if 0
2246 /* FIXME: These only work for ascii chars, and recent smbmount doesn't
2247 allow the flag to be set anyway. Remove? */
2248 switch (server->opt.case_handling) {
2249 case SMB_CASE_UPPER:
2250 str_upper(qname->name, len);
2251 break;
2252 case SMB_CASE_LOWER:
2253 str_lower(qname->name, len);
2254 break;
2255 default:
2256 break;
2257 }
2258#endif
2259
2260 qname->len = 0;
2261 n = server->ops->convert(name_buf, SMB_MAXNAMELEN,
2262 qname->name, len,
2263 server->remote_nls, server->local_nls);
2264 if (n > 0) {
2265 qname->len = n;
2266 qname->name = name_buf;
2267 }
2268
2269out:
2270 return result;
2271}
2272
2273/* findfirst/findnext flags */
2274#define SMB_CLOSE_AFTER_FIRST (1<<0)
2275#define SMB_CLOSE_IF_END (1<<1)
2276#define SMB_REQUIRE_RESUME_KEY (1<<2)
2277#define SMB_CONTINUE_BIT (1<<3)
2278
2279/*
2280 * Note: samba-2.0.7 (at least) has a very similar routine, cli_list, in
2281 * source/libsmb/clilist.c. When looking for smb bugs in the readdir code,
2282 * go there for advise.
2283 *
2284 * Bugs Noted:
2285 * (1) When using Info Level 1 Win NT 4.0 truncates directory listings
2286 * for certain patterns of names and/or lengths. The breakage pattern
2287 * is completely reproducible and can be toggled by the creation of a
2288 * single file. (E.g. echo hi >foo breaks, rm -f foo works.)
2289 */
2290static int
2291smb_proc_readdir_long(struct file *filp, void *dirent, filldir_t filldir,
2292 struct smb_cache_control *ctl)
2293{
2294 struct dentry *dir = filp->f_dentry;
2295 struct smb_sb_info *server = server_from_dentry(dir);
2296 struct qstr qname;
2297 struct smb_fattr fattr;
2298
2299 unsigned char *p, *lastname;
2300 char *mask, *param;
2301 __u16 command;
2302 int first, entries_seen;
2303
2304 /* Both NT and OS/2 accept info level 1 (but see note below). */
2305 int info_level = 260;
2306 const int max_matches = 512;
2307
2308 unsigned int ff_searchcount = 0;
2309 unsigned int ff_eos = 0;
2310 unsigned int ff_lastname = 0;
2311 unsigned int ff_dir_handle = 0;
2312 unsigned int loop_count = 0;
2313 unsigned int mask_len, i;
2314 int result;
2315 struct smb_request *req;
2316 unsigned char *name_buf;
2317 static struct qstr star = {
2318 .name = "*",
2319 .len = 1,
2320 };
2321
2322 lock_kernel();
2323
2324 /*
2325 * We always prefer unix style. Use info level 1 for older
2326 * servers that don't do 260.
2327 */
2328 if (server->opt.capabilities & SMB_CAP_UNIX)
2329 info_level = SMB_FIND_FILE_UNIX;
2330 else if (server->opt.protocol < SMB_PROTOCOL_NT1)
2331 info_level = 1;
2332
2333 result = -ENOMEM;
2334 if (! (name_buf = kmalloc(SMB_MAXNAMELEN+2, GFP_KERNEL)))
2335 goto out;
2336 if (! (req = smb_alloc_request(server, server->opt.max_xmit)))
2337 goto out_name;
2338 param = req->rq_buffer;
2339
2340 /*
2341 * Encode the initial path
2342 */
2343 mask = param + 12;
2344
2345 result = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dir, &star);
2346 if (result <= 0)
2347 goto out_free;
2348 mask_len = result - 1; /* mask_len is strlen, not #bytes */
2349 result = 0;
2350 first = 1;
2351 VERBOSE("starting mask_len=%d, mask=%s\n", mask_len, mask);
2352
2353 entries_seen = 2;
2354 ff_eos = 0;
2355
2356 while (ff_eos == 0) {
2357 loop_count += 1;
2358 if (loop_count > 10) {
2359 printk(KERN_WARNING "smb_proc_readdir_long: "
2360 "Looping in FIND_NEXT??\n");
2361 result = -EIO;
2362 break;
2363 }
2364
2365 if (first != 0) {
2366 command = TRANSACT2_FINDFIRST;
2367 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2368 WSET(param, 2, max_matches); /* max count */
2369 WSET(param, 4, SMB_CLOSE_IF_END);
2370 WSET(param, 6, info_level);
2371 DSET(param, 8, 0);
2372 } else {
2373 command = TRANSACT2_FINDNEXT;
2374
2375 VERBOSE("handle=0x%X, lastname=%d, mask=%.*s\n",
2376 ff_dir_handle, ff_lastname, mask_len, mask);
2377
2378 WSET(param, 0, ff_dir_handle); /* search handle */
2379 WSET(param, 2, max_matches); /* max count */
2380 WSET(param, 4, info_level);
2381 DSET(param, 6, 0);
2382 WSET(param, 10, SMB_CONTINUE_BIT|SMB_CLOSE_IF_END);
2383 }
2384
2385 req->rq_trans2_command = command;
2386 req->rq_ldata = 0;
2387 req->rq_data = NULL;
2388 req->rq_lparm = 12 + mask_len + 1;
2389 req->rq_parm = param;
2390 req->rq_flags = 0;
2391 result = smb_add_request(req);
2392 if (result < 0) {
2393 PARANOIA("error=%d, breaking\n", result);
2394 break;
2395 }
2396
2397 if (req->rq_rcls == ERRSRV && req->rq_err == ERRerror) {
2398 /* a damn Win95 bug - sometimes it clags if you
2399 ask it too fast */
2400 current->state = TASK_INTERRUPTIBLE;
2401 schedule_timeout(HZ/5);
2402 continue;
2403 }
2404
2405 if (req->rq_rcls != 0) {
2406 result = smb_errno(req);
2407 PARANOIA("name=%s, result=%d, rcls=%d, err=%d\n",
2408 mask, result, req->rq_rcls, req->rq_err);
2409 break;
2410 }
2411
2412 /* parse out some important return info */
2413 if (first != 0) {
2414 ff_dir_handle = WVAL(req->rq_parm, 0);
2415 ff_searchcount = WVAL(req->rq_parm, 2);
2416 ff_eos = WVAL(req->rq_parm, 4);
2417 ff_lastname = WVAL(req->rq_parm, 8);
2418 } else {
2419 ff_searchcount = WVAL(req->rq_parm, 0);
2420 ff_eos = WVAL(req->rq_parm, 2);
2421 ff_lastname = WVAL(req->rq_parm, 6);
2422 }
2423
2424 if (ff_searchcount == 0)
2425 break;
2426
2427 /* Now we are ready to parse smb directory entries. */
2428
2429 /* point to the data bytes */
2430 p = req->rq_data;
2431 for (i = 0; i < ff_searchcount; i++) {
2432 /* make sure we stay within the buffer */
2433 if (p >= req->rq_data + req->rq_ldata) {
2434 printk(KERN_ERR "smb_proc_readdir_long: "
2435 "dirent pointer outside buffer! "
2436 "%p %d@%p\n",
2437 p, req->rq_ldata, req->rq_data);
2438 result = -EIO; /* always a comm. error? */
2439 goto out_free;
2440 }
2441
2442 p = smb_decode_long_dirent(server, p, info_level,
2443 &qname, &fattr, name_buf);
2444
2445 /* ignore . and .. from the server */
2446 if (entries_seen == 2 && qname.name[0] == '.') {
2447 if (qname.len == 1)
2448 continue;
2449 if (qname.name[1] == '.' && qname.len == 2)
2450 continue;
2451 }
2452
2453 if (!smb_fill_cache(filp, dirent, filldir, ctl,
2454 &qname, &fattr))
2455 ; /* stop reading? */
2456 entries_seen++;
2457 }
2458
2459 VERBOSE("received %d entries, eos=%d\n", ff_searchcount,ff_eos);
2460
2461 /*
2462 * We might need the lastname for continuations.
2463 *
2464 * Note that some servers (win95?) point to the filename and
2465 * others (NT4, Samba using NT1) to the dir entry. We assume
2466 * here that those who do not point to a filename do not need
2467 * this info to continue the listing.
2468 *
2469 * OS/2 needs this and talks infolevel 1.
2470 * NetApps want lastname with infolevel 260.
2471 * win2k want lastname with infolevel 260, and points to
2472 * the record not to the name.
2473 * Samba+CifsUnixExt doesn't need lastname.
2474 *
2475 * Both are happy if we return the data they point to. So we do.
2476 * (FIXME: above is not true with win2k)
2477 */
2478 mask_len = 0;
2479 if (info_level != SMB_FIND_FILE_UNIX &&
2480 ff_lastname > 0 && ff_lastname < req->rq_ldata) {
2481 lastname = req->rq_data + ff_lastname;
2482
2483 switch (info_level) {
2484 case 260:
2485 mask_len = req->rq_ldata - ff_lastname;
2486 break;
2487 case 1:
2488 /* lastname points to a length byte */
2489 mask_len = *lastname++;
2490 if (ff_lastname + 1 + mask_len > req->rq_ldata)
2491 mask_len = req->rq_ldata - ff_lastname - 1;
2492 break;
2493 }
2494
2495 /*
2496 * Update the mask string for the next message.
2497 */
2498 if (mask_len > 255)
2499 mask_len = 255;
2500 if (mask_len)
2501 strncpy(mask, lastname, mask_len);
2502 }
2503 mask_len = strnlen(mask, mask_len);
2504 VERBOSE("new mask, len=%d@%d of %d, mask=%.*s\n",
2505 mask_len, ff_lastname, req->rq_ldata, mask_len, mask);
2506
2507 first = 0;
2508 loop_count = 0;
2509 }
2510
2511out_free:
2512 smb_rput(req);
2513out_name:
2514 kfree(name_buf);
2515out:
2516 unlock_kernel();
2517 return result;
2518}
2519
2520/*
2521 * This version uses the trans2 TRANSACT2_FINDFIRST message
2522 * to get the attribute data.
2523 *
2524 * Bugs Noted:
2525 */
2526static int
2527smb_proc_getattr_ff(struct smb_sb_info *server, struct dentry *dentry,
2528 struct smb_fattr *fattr)
2529{
2530 char *param, *mask;
2531 __u16 date, time;
2532 int mask_len, result;
2533 struct smb_request *req;
2534
2535 result = -ENOMEM;
2536 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2537 goto out;
2538 param = req->rq_buffer;
2539 mask = param + 12;
2540
2541 mask_len = smb_encode_path(server, mask, SMB_MAXPATHLEN+1, dentry,NULL);
2542 if (mask_len < 0) {
2543 result = mask_len;
2544 goto out_free;
2545 }
2546 VERBOSE("name=%s, len=%d\n", mask, mask_len);
2547 WSET(param, 0, aSYSTEM | aHIDDEN | aDIR);
2548 WSET(param, 2, 1); /* max count */
2549 WSET(param, 4, 1); /* close after this call */
2550 WSET(param, 6, 1); /* info_level */
2551 DSET(param, 8, 0);
2552
2553 req->rq_trans2_command = TRANSACT2_FINDFIRST;
2554 req->rq_ldata = 0;
2555 req->rq_data = NULL;
2556 req->rq_lparm = 12 + mask_len;
2557 req->rq_parm = param;
2558 req->rq_flags = 0;
2559 result = smb_add_request(req);
2560 if (result < 0)
2561 goto out_free;
2562 if (req->rq_rcls != 0) {
2563 result = smb_errno(req);
2564#ifdef SMBFS_PARANOIA
2565 if (result != -ENOENT)
2566 PARANOIA("error for %s, rcls=%d, err=%d\n",
2567 mask, req->rq_rcls, req->rq_err);
2568#endif
2569 goto out_free;
2570 }
2571 /* Make sure we got enough data ... */
2572 result = -EINVAL;
2573 if (req->rq_ldata < 22 || WVAL(req->rq_parm, 2) != 1) {
2574 PARANOIA("bad result for %s, len=%d, count=%d\n",
2575 mask, req->rq_ldata, WVAL(req->rq_parm, 2));
2576 goto out_free;
2577 }
2578
2579 /*
2580 * Decode the response into the fattr ...
2581 */
2582 date = WVAL(req->rq_data, 0);
2583 time = WVAL(req->rq_data, 2);
2584 fattr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2585 fattr->f_ctime.tv_nsec = 0;
2586
2587 date = WVAL(req->rq_data, 4);
2588 time = WVAL(req->rq_data, 6);
2589 fattr->f_atime.tv_sec = date_dos2unix(server, date, time);
2590 fattr->f_atime.tv_nsec = 0;
2591
2592 date = WVAL(req->rq_data, 8);
2593 time = WVAL(req->rq_data, 10);
2594 fattr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2595 fattr->f_mtime.tv_nsec = 0;
2596 VERBOSE("name=%s, date=%x, time=%x, mtime=%ld\n",
2597 mask, date, time, fattr->f_mtime);
2598 fattr->f_size = DVAL(req->rq_data, 12);
2599 /* ULONG allocation size */
2600 fattr->attr = WVAL(req->rq_data, 20);
2601 result = 0;
2602
2603out_free:
2604 smb_rput(req);
2605out:
2606 return result;
2607}
2608
2609static int
2610smb_proc_getattr_core(struct smb_sb_info *server, struct dentry *dir,
2611 struct smb_fattr *fattr)
2612{
2613 int result;
2614 char *p;
2615 struct smb_request *req;
2616
2617 result = -ENOMEM;
2618 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2619 goto out;
2620
2621 p = smb_setup_header(req, SMBgetatr, 0, 0);
2622 result = smb_simple_encode_path(req, &p, dir, NULL);
2623 if (result < 0)
2624 goto out_free;
2625 smb_setup_bcc(req, p);
2626
2627 if ((result = smb_request_ok(req, SMBgetatr, 10, 0)) < 0)
2628 goto out_free;
2629 fattr->attr = WVAL(req->rq_header, smb_vwv0);
2630 fattr->f_mtime.tv_sec = local2utc(server, DVAL(req->rq_header, smb_vwv1));
2631 fattr->f_mtime.tv_nsec = 0;
2632 fattr->f_size = DVAL(req->rq_header, smb_vwv3);
2633 fattr->f_ctime = fattr->f_mtime;
2634 fattr->f_atime = fattr->f_mtime;
2635#ifdef SMBFS_DEBUG_TIMESTAMP
2636 printk("getattr_core: %s/%s, mtime=%ld\n",
2637 DENTRY_PATH(dir), fattr->f_mtime);
2638#endif
2639 result = 0;
2640
2641out_free:
2642 smb_rput(req);
2643out:
2644 return result;
2645}
2646
2647/*
2648 * Bugs Noted:
2649 * (1) Win 95 swaps the date and time fields in the standard info level.
2650 */
2651static int
2652smb_proc_getattr_trans2(struct smb_sb_info *server, struct dentry *dir,
2653 struct smb_request *req, int infolevel)
2654{
2655 char *p, *param;
2656 int result;
2657
2658 param = req->rq_buffer;
2659 WSET(param, 0, infolevel);
2660 DSET(param, 2, 0);
2661 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
2662 if (result < 0)
2663 goto out;
2664 p = param + 6 + result;
2665
2666 req->rq_trans2_command = TRANSACT2_QPATHINFO;
2667 req->rq_ldata = 0;
2668 req->rq_data = NULL;
2669 req->rq_lparm = p - param;
2670 req->rq_parm = param;
2671 req->rq_flags = 0;
2672 result = smb_add_request(req);
2673 if (result < 0)
2674 goto out;
2675 if (req->rq_rcls != 0) {
2676 VERBOSE("for %s: result=%d, rcls=%d, err=%d\n",
2677 &param[6], result, req->rq_rcls, req->rq_err);
2678 result = smb_errno(req);
2679 goto out;
2680 }
2681 result = -ENOENT;
2682 if (req->rq_ldata < 22) {
2683 PARANOIA("not enough data for %s, len=%d\n",
2684 &param[6], req->rq_ldata);
2685 goto out;
2686 }
2687
2688 result = 0;
2689out:
2690 return result;
2691}
2692
2693static int
2694smb_proc_getattr_trans2_std(struct smb_sb_info *server, struct dentry *dir,
2695 struct smb_fattr *attr)
2696{
2697 u16 date, time;
2698 int off_date = 0, off_time = 2;
2699 int result;
2700 struct smb_request *req;
2701
2702 result = -ENOMEM;
2703 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2704 goto out;
2705
2706 result = smb_proc_getattr_trans2(server, dir, req, SMB_INFO_STANDARD);
2707 if (result < 0)
2708 goto out_free;
2709
2710 /*
2711 * Kludge alert: Win 95 swaps the date and time field,
2712 * contrary to the CIFS docs and Win NT practice.
2713 */
2714 if (server->mnt->flags & SMB_MOUNT_WIN95) {
2715 off_date = 2;
2716 off_time = 0;
2717 }
2718 date = WVAL(req->rq_data, off_date);
2719 time = WVAL(req->rq_data, off_time);
2720 attr->f_ctime.tv_sec = date_dos2unix(server, date, time);
2721 attr->f_ctime.tv_nsec = 0;
2722
2723 date = WVAL(req->rq_data, 4 + off_date);
2724 time = WVAL(req->rq_data, 4 + off_time);
2725 attr->f_atime.tv_sec = date_dos2unix(server, date, time);
2726 attr->f_atime.tv_nsec = 0;
2727
2728 date = WVAL(req->rq_data, 8 + off_date);
2729 time = WVAL(req->rq_data, 8 + off_time);
2730 attr->f_mtime.tv_sec = date_dos2unix(server, date, time);
2731 attr->f_mtime.tv_nsec = 0;
2732#ifdef SMBFS_DEBUG_TIMESTAMP
2733 printk(KERN_DEBUG "getattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
2734 DENTRY_PATH(dir), date, time, attr->f_mtime);
2735#endif
2736 attr->f_size = DVAL(req->rq_data, 12);
2737 attr->attr = WVAL(req->rq_data, 20);
2738
2739out_free:
2740 smb_rput(req);
2741out:
2742 return result;
2743}
2744
2745static int
2746smb_proc_getattr_trans2_all(struct smb_sb_info *server, struct dentry *dir,
2747 struct smb_fattr *attr)
2748{
2749 struct smb_request *req;
2750 int result;
2751
2752 result = -ENOMEM;
2753 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2754 goto out;
2755
2756 result = smb_proc_getattr_trans2(server, dir, req,
2757 SMB_QUERY_FILE_ALL_INFO);
2758 if (result < 0)
2759 goto out_free;
2760
2761 attr->f_ctime = smb_ntutc2unixutc(LVAL(req->rq_data, 0));
2762 attr->f_atime = smb_ntutc2unixutc(LVAL(req->rq_data, 8));
2763 attr->f_mtime = smb_ntutc2unixutc(LVAL(req->rq_data, 16));
2764 /* change (24) */
2765 attr->attr = WVAL(req->rq_data, 32);
2766 /* pad? (34) */
2767 /* allocated size (40) */
2768 attr->f_size = LVAL(req->rq_data, 48);
2769
2770out_free:
2771 smb_rput(req);
2772out:
2773 return result;
2774}
2775
2776static int
2777smb_proc_getattr_unix(struct smb_sb_info *server, struct dentry *dir,
2778 struct smb_fattr *attr)
2779{
2780 struct smb_request *req;
2781 int result;
2782
2783 result = -ENOMEM;
2784 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2785 goto out;
2786
2787 result = smb_proc_getattr_trans2(server, dir, req,
2788 SMB_QUERY_FILE_UNIX_BASIC);
2789 if (result < 0)
2790 goto out_free;
2791
2792 smb_decode_unix_basic(attr, server, req->rq_data);
2793
2794out_free:
2795 smb_rput(req);
2796out:
2797 return result;
2798}
2799
2800static int
2801smb_proc_getattr_95(struct smb_sb_info *server, struct dentry *dir,
2802 struct smb_fattr *attr)
2803{
2804 struct inode *inode = dir->d_inode;
2805 int result;
2806
2807 /* FIXME: why not use the "all" version? */
2808 result = smb_proc_getattr_trans2_std(server, dir, attr);
2809 if (result < 0)
2810 goto out;
2811
2812 /*
2813 * None of the getattr versions here can make win9x return the right
2814 * filesize if there are changes made to an open file.
2815 * A seek-to-end does return the right size, but we only need to do
2816 * that on files we have written.
2817 */
2818 if (inode && SMB_I(inode)->flags & SMB_F_LOCALWRITE &&
2819 smb_is_open(inode))
2820 {
2821 __u16 fileid = SMB_I(inode)->fileid;
2822 attr->f_size = smb_proc_seek(server, fileid, 2, 0);
2823 }
2824
2825out:
2826 return result;
2827}
2828
2829static int
2830smb_proc_ops_wait(struct smb_sb_info *server)
2831{
2832 int result;
2833
2834 result = wait_event_interruptible_timeout(server->conn_wq,
2835 server->conn_complete, 30*HZ);
2836
2837 if (!result || signal_pending(current))
2838 return -EIO;
2839
2840 return 0;
2841}
2842
2843static int
2844smb_proc_getattr_null(struct smb_sb_info *server, struct dentry *dir,
2845 struct smb_fattr *fattr)
2846{
2847 int result;
2848
2849 if (smb_proc_ops_wait(server) < 0)
2850 return -EIO;
2851
2852 smb_init_dirent(server, fattr);
2853 result = server->ops->getattr(server, dir, fattr);
2854 smb_finish_dirent(server, fattr);
2855
2856 return result;
2857}
2858
2859static int
2860smb_proc_readdir_null(struct file *filp, void *dirent, filldir_t filldir,
2861 struct smb_cache_control *ctl)
2862{
2863 struct smb_sb_info *server = server_from_dentry(filp->f_dentry);
2864
2865 if (smb_proc_ops_wait(server) < 0)
2866 return -EIO;
2867
2868 return server->ops->readdir(filp, dirent, filldir, ctl);
2869}
2870
2871int
2872smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr)
2873{
2874 struct smb_sb_info *server = server_from_dentry(dir);
2875 int result;
2876
2877 smb_init_dirent(server, fattr);
2878 result = server->ops->getattr(server, dir, fattr);
2879 smb_finish_dirent(server, fattr);
2880
2881 return result;
2882}
2883
2884
2885/*
2886 * Because of bugs in the core protocol, we use this only to set
2887 * attributes. See smb_proc_settime() below for timestamp handling.
2888 *
2889 * Bugs Noted:
2890 * (1) If mtime is non-zero, both Win 3.1 and Win 95 fail
2891 * with an undocumented error (ERRDOS code 50). Setting
2892 * mtime to 0 allows the attributes to be set.
2893 * (2) The extra parameters following the name string aren't
2894 * in the CIFS docs, but seem to be necessary for operation.
2895 */
2896static int
2897smb_proc_setattr_core(struct smb_sb_info *server, struct dentry *dentry,
2898 __u16 attr)
2899{
2900 char *p;
2901 int result;
2902 struct smb_request *req;
2903
2904 result = -ENOMEM;
2905 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
2906 goto out;
2907
2908 p = smb_setup_header(req, SMBsetatr, 8, 0);
2909 WSET(req->rq_header, smb_vwv0, attr);
2910 DSET(req->rq_header, smb_vwv1, 0); /* mtime */
2911 WSET(req->rq_header, smb_vwv3, 0); /* reserved values */
2912 WSET(req->rq_header, smb_vwv4, 0);
2913 WSET(req->rq_header, smb_vwv5, 0);
2914 WSET(req->rq_header, smb_vwv6, 0);
2915 WSET(req->rq_header, smb_vwv7, 0);
2916 result = smb_simple_encode_path(req, &p, dentry, NULL);
2917 if (result < 0)
2918 goto out_free;
2919 if (p + 2 > (char *)req->rq_buffer + req->rq_bufsize) {
2920 result = -ENAMETOOLONG;
2921 goto out_free;
2922 }
2923 *p++ = 4;
2924 *p++ = 0;
2925 smb_setup_bcc(req, p);
2926
2927 result = smb_request_ok(req, SMBsetatr, 0, 0);
2928 if (result < 0)
2929 goto out_free;
2930 result = 0;
2931
2932out_free:
2933 smb_rput(req);
2934out:
2935 return result;
2936}
2937
2938/*
2939 * Because of bugs in the trans2 setattr messages, we must set
2940 * attributes and timestamps separately. The core SMBsetatr
2941 * message seems to be the only reliable way to set attributes.
2942 */
2943int
2944smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr)
2945{
2946 struct smb_sb_info *server = server_from_dentry(dir);
2947 int result;
2948
2949 VERBOSE("setting %s/%s, open=%d\n",
2950 DENTRY_PATH(dir), smb_is_open(dir->d_inode));
2951 result = smb_proc_setattr_core(server, dir, fattr->attr);
2952 return result;
2953}
2954
2955/*
2956 * Sets the timestamps for an file open with write permissions.
2957 */
2958static int
2959smb_proc_setattr_ext(struct smb_sb_info *server,
2960 struct inode *inode, struct smb_fattr *fattr)
2961{
2962 __u16 date, time;
2963 int result;
2964 struct smb_request *req;
2965
2966 result = -ENOMEM;
2967 if (! (req = smb_alloc_request(server, 0)))
2968 goto out;
2969
2970 smb_setup_header(req, SMBsetattrE, 7, 0);
2971 WSET(req->rq_header, smb_vwv0, SMB_I(inode)->fileid);
2972 /* We don't change the creation time */
2973 WSET(req->rq_header, smb_vwv1, 0);
2974 WSET(req->rq_header, smb_vwv2, 0);
2975 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
2976 WSET(req->rq_header, smb_vwv3, date);
2977 WSET(req->rq_header, smb_vwv4, time);
2978 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
2979 WSET(req->rq_header, smb_vwv5, date);
2980 WSET(req->rq_header, smb_vwv6, time);
2981#ifdef SMBFS_DEBUG_TIMESTAMP
2982 printk(KERN_DEBUG "smb_proc_setattr_ext: date=%d, time=%d, mtime=%ld\n",
2983 date, time, fattr->f_mtime);
2984#endif
2985
2986 req->rq_flags |= SMB_REQ_NORETRY;
2987 result = smb_request_ok(req, SMBsetattrE, 0, 0);
2988 if (result < 0)
2989 goto out_free;
2990 result = 0;
2991out_free:
2992 smb_rput(req);
2993out:
2994 return result;
2995}
2996
2997/*
2998 * Bugs Noted:
2999 * (1) The TRANSACT2_SETPATHINFO message under Win NT 4.0 doesn't
3000 * set the file's attribute flags.
3001 */
3002static int
3003smb_proc_setattr_trans2(struct smb_sb_info *server,
3004 struct dentry *dir, struct smb_fattr *fattr)
3005{
3006 __u16 date, time;
3007 char *p, *param;
3008 int result;
3009 char data[26];
3010 struct smb_request *req;
3011
3012 result = -ENOMEM;
3013 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3014 goto out;
3015 param = req->rq_buffer;
3016
3017 WSET(param, 0, 1); /* Info level SMB_INFO_STANDARD */
3018 DSET(param, 2, 0);
3019 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, dir, NULL);
3020 if (result < 0)
3021 goto out_free;
3022 p = param + 6 + result;
3023
3024 WSET(data, 0, 0); /* creation time */
3025 WSET(data, 2, 0);
3026 date_unix2dos(server, fattr->f_atime.tv_sec, &date, &time);
3027 WSET(data, 4, date);
3028 WSET(data, 6, time);
3029 date_unix2dos(server, fattr->f_mtime.tv_sec, &date, &time);
3030 WSET(data, 8, date);
3031 WSET(data, 10, time);
3032#ifdef SMBFS_DEBUG_TIMESTAMP
3033 printk(KERN_DEBUG "setattr_trans2: %s/%s, date=%x, time=%x, mtime=%ld\n",
3034 DENTRY_PATH(dir), date, time, fattr->f_mtime);
3035#endif
3036 DSET(data, 12, 0); /* size */
3037 DSET(data, 16, 0); /* blksize */
3038 WSET(data, 20, 0); /* attr */
3039 DSET(data, 22, 0); /* ULONG EA size */
3040
3041 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3042 req->rq_ldata = 26;
3043 req->rq_data = data;
3044 req->rq_lparm = p - param;
3045 req->rq_parm = param;
3046 req->rq_flags = 0;
3047 result = smb_add_request(req);
3048 if (result < 0)
3049 goto out_free;
3050 result = 0;
3051 if (req->rq_rcls != 0)
3052 result = smb_errno(req);
3053
3054out_free:
3055 smb_rput(req);
3056out:
3057 return result;
3058}
3059
3060/*
3061 * ATTR_MODE 0x001
3062 * ATTR_UID 0x002
3063 * ATTR_GID 0x004
3064 * ATTR_SIZE 0x008
3065 * ATTR_ATIME 0x010
3066 * ATTR_MTIME 0x020
3067 * ATTR_CTIME 0x040
3068 * ATTR_ATIME_SET 0x080
3069 * ATTR_MTIME_SET 0x100
3070 * ATTR_FORCE 0x200
3071 * ATTR_ATTR_FLAG 0x400
3072 *
3073 * major/minor should only be set by mknod.
3074 */
3075int
3076smb_proc_setattr_unix(struct dentry *d, struct iattr *attr,
3077 unsigned int major, unsigned int minor)
3078{
3079 struct smb_sb_info *server = server_from_dentry(d);
3080 u64 nttime;
3081 char *p, *param;
3082 int result;
3083 char data[100];
3084 struct smb_request *req;
3085
3086 result = -ENOMEM;
3087 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3088 goto out;
3089 param = req->rq_buffer;
3090
3091 DEBUG1("valid flags = 0x%04x\n", attr->ia_valid);
3092
3093 WSET(param, 0, SMB_SET_FILE_UNIX_BASIC);
3094 DSET(param, 2, 0);
3095 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3096 if (result < 0)
3097 goto out_free;
3098 p = param + 6 + result;
3099
3100 /* 0 L file size in bytes */
3101 /* 8 L file size on disk in bytes (block count) */
3102 /* 40 L uid */
3103 /* 48 L gid */
3104 /* 56 W file type enum */
3105 /* 60 L devmajor */
3106 /* 68 L devminor */
3107 /* 76 L unique ID (inode) */
3108 /* 84 L permissions */
3109 /* 92 L link count */
3110 LSET(data, 0, SMB_SIZE_NO_CHANGE);
3111 LSET(data, 8, SMB_SIZE_NO_CHANGE);
3112 LSET(data, 16, SMB_TIME_NO_CHANGE);
3113 LSET(data, 24, SMB_TIME_NO_CHANGE);
3114 LSET(data, 32, SMB_TIME_NO_CHANGE);
3115 LSET(data, 40, SMB_UID_NO_CHANGE);
3116 LSET(data, 48, SMB_GID_NO_CHANGE);
3117 LSET(data, 56, smb_filetype_from_mode(attr->ia_mode));
3118 LSET(data, 60, major);
3119 LSET(data, 68, minor);
3120 LSET(data, 76, 0);
3121 LSET(data, 84, SMB_MODE_NO_CHANGE);
3122 LSET(data, 92, 0);
3123
3124 if (attr->ia_valid & ATTR_SIZE) {
3125 LSET(data, 0, attr->ia_size);
3126 LSET(data, 8, 0); /* can't set anyway */
3127 }
3128
3129 /*
3130 * FIXME: check the conversion function it the correct one
3131 *
3132 * we can't set ctime but we might as well pass this to the server
3133 * and let it ignore it.
3134 */
3135 if (attr->ia_valid & ATTR_CTIME) {
3136 nttime = smb_unixutc2ntutc(attr->ia_ctime);
3137 LSET(data, 16, nttime);
3138 }
3139 if (attr->ia_valid & ATTR_ATIME) {
3140 nttime = smb_unixutc2ntutc(attr->ia_atime);
3141 LSET(data, 24, nttime);
3142 }
3143 if (attr->ia_valid & ATTR_MTIME) {
3144 nttime = smb_unixutc2ntutc(attr->ia_mtime);
3145 LSET(data, 32, nttime);
3146 }
3147
3148 if (attr->ia_valid & ATTR_UID) {
3149 LSET(data, 40, attr->ia_uid);
3150 }
3151 if (attr->ia_valid & ATTR_GID) {
3152 LSET(data, 48, attr->ia_gid);
3153 }
3154
3155 if (attr->ia_valid & ATTR_MODE) {
3156 LSET(data, 84, attr->ia_mode);
3157 }
3158
3159 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3160 req->rq_ldata = 100;
3161 req->rq_data = data;
3162 req->rq_lparm = p - param;
3163 req->rq_parm = param;
3164 req->rq_flags = 0;
3165 result = smb_add_request(req);
3166
3167out_free:
3168 smb_rput(req);
3169out:
3170 return result;
3171}
3172
3173
3174/*
3175 * Set the modify and access timestamps for a file.
3176 *
3177 * Incredibly enough, in all of SMB there is no message to allow
3178 * setting both attributes and timestamps at once.
3179 *
3180 * Bugs Noted:
3181 * (1) Win 95 doesn't support the TRANSACT2_SETFILEINFO message
3182 * with info level 1 (INFO_STANDARD).
3183 * (2) Win 95 seems not to support setting directory timestamps.
3184 * (3) Under the core protocol apparently the only way to set the
3185 * timestamp is to open and close the file.
3186 */
3187int
3188smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr)
3189{
3190 struct smb_sb_info *server = server_from_dentry(dentry);
3191 struct inode *inode = dentry->d_inode;
3192 int result;
3193
3194 VERBOSE("setting %s/%s, open=%d\n",
3195 DENTRY_PATH(dentry), smb_is_open(inode));
3196
3197 /* setting the time on a Win95 server fails (tridge) */
3198 if (server->opt.protocol >= SMB_PROTOCOL_LANMAN2 &&
3199 !(server->mnt->flags & SMB_MOUNT_WIN95)) {
3200 if (smb_is_open(inode) && SMB_I(inode)->access != SMB_O_RDONLY)
3201 result = smb_proc_setattr_ext(server, inode, fattr);
3202 else
3203 result = smb_proc_setattr_trans2(server, dentry, fattr);
3204 } else {
3205 /*
3206 * Fail silently on directories ... timestamp can't be set?
3207 */
3208 result = 0;
3209 if (S_ISREG(inode->i_mode)) {
3210 /*
3211 * Set the mtime by opening and closing the file.
3212 * Note that the file is opened read-only, but this
3213 * still allows us to set the date (tridge)
3214 */
3215 result = -EACCES;
3216 if (!smb_is_open(inode))
3217 smb_proc_open(server, dentry, SMB_O_RDONLY);
3218 if (smb_is_open(inode)) {
3219 inode->i_mtime = fattr->f_mtime;
3220 result = smb_proc_close_inode(server, inode);
3221 }
3222 }
3223 }
3224
3225 return result;
3226}
3227
3228int
3229smb_proc_dskattr(struct super_block *sb, struct kstatfs *attr)
3230{
3231 struct smb_sb_info *server = SMB_SB(sb);
3232 int result;
3233 char *p;
3234 long unit;
3235 struct smb_request *req;
3236
3237 result = -ENOMEM;
3238 if (! (req = smb_alloc_request(server, 0)))
3239 goto out;
3240
3241 smb_setup_header(req, SMBdskattr, 0, 0);
3242 if ((result = smb_request_ok(req, SMBdskattr, 5, 0)) < 0)
3243 goto out_free;
3244 p = SMB_VWV(req->rq_header);
3245 unit = (WVAL(p, 2) * WVAL(p, 4)) >> SMB_ST_BLKSHIFT;
3246 attr->f_blocks = WVAL(p, 0) * unit;
3247 attr->f_bsize = SMB_ST_BLKSIZE;
3248 attr->f_bavail = attr->f_bfree = WVAL(p, 6) * unit;
3249 result = 0;
3250
3251out_free:
3252 smb_rput(req);
3253out:
3254 return result;
3255}
3256
3257int
3258smb_proc_read_link(struct smb_sb_info *server, struct dentry *d,
3259 char *buffer, int len)
3260{
3261 char *p, *param;
3262 int result;
3263 struct smb_request *req;
3264
3265 DEBUG1("readlink of %s/%s\n", DENTRY_PATH(d));
3266
3267 result = -ENOMEM;
3268 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3269 goto out;
3270 param = req->rq_buffer;
3271
3272 WSET(param, 0, SMB_QUERY_FILE_UNIX_LINK);
3273 DSET(param, 2, 0);
3274 result = smb_encode_path(server, param+6, SMB_MAXPATHLEN+1, d, NULL);
3275 if (result < 0)
3276 goto out_free;
3277 p = param + 6 + result;
3278
3279 req->rq_trans2_command = TRANSACT2_QPATHINFO;
3280 req->rq_ldata = 0;
3281 req->rq_data = NULL;
3282 req->rq_lparm = p - param;
3283 req->rq_parm = param;
3284 req->rq_flags = 0;
3285 result = smb_add_request(req);
3286 if (result < 0)
3287 goto out_free;
3288 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3289 &param[6], result, req->rq_rcls, req->rq_err);
3290
3291 /* copy data up to the \0 or buffer length */
3292 result = len;
3293 if (req->rq_ldata < len)
3294 result = req->rq_ldata;
3295 strncpy(buffer, req->rq_data, result);
3296
3297out_free:
3298 smb_rput(req);
3299out:
3300 return result;
3301}
3302
3303
3304/*
3305 * Create a symlink object called dentry which points to oldpath.
3306 * Samba does not permit dangling links but returns a suitable error message.
3307 */
3308int
3309smb_proc_symlink(struct smb_sb_info *server, struct dentry *d,
3310 const char *oldpath)
3311{
3312 char *p, *param;
3313 int result;
3314 struct smb_request *req;
3315
3316 result = -ENOMEM;
3317 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3318 goto out;
3319 param = req->rq_buffer;
3320
3321 WSET(param, 0, SMB_SET_FILE_UNIX_LINK);
3322 DSET(param, 2, 0);
3323 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1, d, NULL);
3324 if (result < 0)
3325 goto out_free;
3326 p = param + 6 + result;
3327
3328 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3329 req->rq_ldata = strlen(oldpath) + 1;
3330 req->rq_data = (char *) oldpath;
3331 req->rq_lparm = p - param;
3332 req->rq_parm = param;
3333 req->rq_flags = 0;
3334 result = smb_add_request(req);
3335 if (result < 0)
3336 goto out_free;
3337
3338 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3339 &param[6], result, req->rq_rcls, req->rq_err);
3340 result = 0;
3341
3342out_free:
3343 smb_rput(req);
3344out:
3345 return result;
3346}
3347
3348/*
3349 * Create a hard link object called new_dentry which points to dentry.
3350 */
3351int
3352smb_proc_link(struct smb_sb_info *server, struct dentry *dentry,
3353 struct dentry *new_dentry)
3354{
3355 char *p, *param;
3356 int result;
3357 struct smb_request *req;
3358
3359 result = -ENOMEM;
3360 if (! (req = smb_alloc_request(server, PAGE_SIZE)))
3361 goto out;
3362 param = req->rq_buffer;
3363
3364 WSET(param, 0, SMB_SET_FILE_UNIX_HLINK);
3365 DSET(param, 2, 0);
3366 result = smb_encode_path(server, param + 6, SMB_MAXPATHLEN+1,
3367 new_dentry, NULL);
3368 if (result < 0)
3369 goto out_free;
3370 p = param + 6 + result;
3371
3372 /* Grr, pointless separation of parameters and data ... */
3373 req->rq_data = p;
3374 req->rq_ldata = smb_encode_path(server, p, SMB_MAXPATHLEN+1,
3375 dentry, NULL);
3376
3377 req->rq_trans2_command = TRANSACT2_SETPATHINFO;
3378 req->rq_lparm = p - param;
3379 req->rq_parm = param;
3380 req->rq_flags = 0;
3381 result = smb_add_request(req);
3382 if (result < 0)
3383 goto out_free;
3384
3385 DEBUG1("for %s: result=%d, rcls=%d, err=%d\n",
3386 &param[6], result, req->rq_rcls, req->rq_err);
3387 result = 0;
3388
3389out_free:
3390 smb_rput(req);
3391out:
3392 return result;
3393}
3394
3395static int
3396smb_proc_query_cifsunix(struct smb_sb_info *server)
3397{
3398 int result;
3399 int major, minor;
3400 u64 caps;
3401 char param[2];
3402 struct smb_request *req;
3403
3404 result = -ENOMEM;
3405 if (! (req = smb_alloc_request(server, 100)))
3406 goto out;
3407
3408 WSET(param, 0, SMB_QUERY_CIFS_UNIX_INFO);
3409
3410 req->rq_trans2_command = TRANSACT2_QFSINFO;
3411 req->rq_ldata = 0;
3412 req->rq_data = NULL;
3413 req->rq_lparm = 2;
3414 req->rq_parm = param;
3415 req->rq_flags = 0;
3416 result = smb_add_request(req);
3417 if (result < 0)
3418 goto out_free;
3419
3420 if (req->rq_ldata < 12) {
3421 PARANOIA("Not enough data\n");
3422 goto out_free;
3423 }
3424 major = WVAL(req->rq_data, 0);
3425 minor = WVAL(req->rq_data, 2);
3426
3427 DEBUG1("Server implements CIFS Extensions for UNIX systems v%d.%d\n",
3428 major, minor);
3429 /* FIXME: verify that we are ok with this major/minor? */
3430
3431 caps = LVAL(req->rq_data, 4);
3432 DEBUG1("Server capabilities 0x%016llx\n", caps);
3433
3434out_free:
3435 smb_rput(req);
3436out:
3437 return result;
3438}
3439
3440
3441static void
3442install_ops(struct smb_ops *dst, struct smb_ops *src)
3443{
3444 memcpy(dst, src, sizeof(void *) * SMB_OPS_NUM_STATIC);
3445}
3446
3447/* < LANMAN2 */
3448static struct smb_ops smb_ops_core =
3449{
3450 .read = smb_proc_read,
3451 .write = smb_proc_write,
3452 .readdir = smb_proc_readdir_short,
3453 .getattr = smb_proc_getattr_core,
3454 .truncate = smb_proc_trunc32,
3455};
3456
3457/* LANMAN2, OS/2, others? */
3458static struct smb_ops smb_ops_os2 =
3459{
3460 .read = smb_proc_read,
3461 .write = smb_proc_write,
3462 .readdir = smb_proc_readdir_long,
3463 .getattr = smb_proc_getattr_trans2_std,
3464 .truncate = smb_proc_trunc32,
3465};
3466
3467/* Win95, and possibly some NetApp versions too */
3468static struct smb_ops smb_ops_win95 =
3469{
3470 .read = smb_proc_read, /* does not support 12word readX */
3471 .write = smb_proc_write,
3472 .readdir = smb_proc_readdir_long,
3473 .getattr = smb_proc_getattr_95,
3474 .truncate = smb_proc_trunc95,
3475};
3476
3477/* Samba, NT4 and NT5 */
3478static struct smb_ops smb_ops_winNT =
3479{
3480 .read = smb_proc_readX,
3481 .write = smb_proc_writeX,
3482 .readdir = smb_proc_readdir_long,
3483 .getattr = smb_proc_getattr_trans2_all,
3484 .truncate = smb_proc_trunc64,
3485};
3486
3487/* Samba w/ unix extensions. Others? */
3488static struct smb_ops smb_ops_unix =
3489{
3490 .read = smb_proc_readX,
3491 .write = smb_proc_writeX,
3492 .readdir = smb_proc_readdir_long,
3493 .getattr = smb_proc_getattr_unix,
3494 /* FIXME: core/ext/time setattr needs to be cleaned up! */
3495 /* .setattr = smb_proc_setattr_unix, */
3496 .truncate = smb_proc_trunc64,
3497};
3498
3499/* Place holder until real ops are in place */
3500static struct smb_ops smb_ops_null =
3501{
3502 .readdir = smb_proc_readdir_null,
3503 .getattr = smb_proc_getattr_null,
3504};
3505
3506void smb_install_null_ops(struct smb_ops *ops)
3507{
3508 install_ops(ops, &smb_ops_null);
3509}
diff --git a/fs/smbfs/proto.h b/fs/smbfs/proto.h
new file mode 100644
index 000000000000..e866ec8660d0
--- /dev/null
+++ b/fs/smbfs/proto.h
@@ -0,0 +1,87 @@
1/*
2 * Autogenerated with cproto on: Sat Sep 13 17:18:51 CEST 2003
3 */
4
5struct smb_request;
6struct sock;
7struct statfs;
8
9/* proc.c */
10extern int smb_setcodepage(struct smb_sb_info *server, struct smb_nls_codepage *cp);
11extern __u32 smb_len(__u8 *p);
12extern int smb_get_rsize(struct smb_sb_info *server);
13extern int smb_get_wsize(struct smb_sb_info *server);
14extern int smb_errno(struct smb_request *req);
15extern int smb_newconn(struct smb_sb_info *server, struct smb_conn_opt *opt);
16extern __u8 *smb_setup_header(struct smb_request *req, __u8 command, __u16 wct, __u16 bcc);
17extern int smb_open(struct dentry *dentry, int wish);
18extern int smb_close(struct inode *ino);
19extern int smb_close_fileid(struct dentry *dentry, __u16 fileid);
20extern int smb_proc_create(struct dentry *dentry, __u16 attr, time_t ctime, __u16 *fileid);
21extern int smb_proc_mv(struct dentry *old_dentry, struct dentry *new_dentry);
22extern int smb_proc_mkdir(struct dentry *dentry);
23extern int smb_proc_rmdir(struct dentry *dentry);
24extern int smb_proc_unlink(struct dentry *dentry);
25extern int smb_proc_flush(struct smb_sb_info *server, __u16 fileid);
26extern void smb_init_root_dirent(struct smb_sb_info *server, struct smb_fattr *fattr,
27 struct super_block *sb);
28extern int smb_proc_getattr(struct dentry *dir, struct smb_fattr *fattr);
29extern int smb_proc_setattr(struct dentry *dir, struct smb_fattr *fattr);
30extern int smb_proc_setattr_unix(struct dentry *d, struct iattr *attr, unsigned int major, unsigned int minor);
31extern int smb_proc_settime(struct dentry *dentry, struct smb_fattr *fattr);
32extern int smb_proc_dskattr(struct super_block *sb, struct kstatfs *attr);
33extern int smb_proc_read_link(struct smb_sb_info *server, struct dentry *d, char *buffer, int len);
34extern int smb_proc_symlink(struct smb_sb_info *server, struct dentry *d, const char *oldpath);
35extern int smb_proc_link(struct smb_sb_info *server, struct dentry *dentry, struct dentry *new_dentry);
36extern void smb_install_null_ops(struct smb_ops *ops);
37/* dir.c */
38extern struct file_operations smb_dir_operations;
39extern struct inode_operations smb_dir_inode_operations;
40extern struct inode_operations smb_dir_inode_operations_unix;
41extern void smb_new_dentry(struct dentry *dentry);
42extern void smb_renew_times(struct dentry *dentry);
43/* cache.c */
44extern void smb_invalid_dir_cache(struct inode *dir);
45extern void smb_invalidate_dircache_entries(struct dentry *parent);
46extern struct dentry *smb_dget_fpos(struct dentry *dentry, struct dentry *parent, unsigned long fpos);
47extern int smb_fill_cache(struct file *filp, void *dirent, filldir_t filldir, struct smb_cache_control *ctrl, struct qstr *qname, struct smb_fattr *entry);
48/* sock.c */
49extern void smb_data_ready(struct sock *sk, int len);
50extern int smb_valid_socket(struct inode *inode);
51extern void smb_close_socket(struct smb_sb_info *server);
52extern int smb_recv_available(struct smb_sb_info *server);
53extern int smb_receive_header(struct smb_sb_info *server);
54extern int smb_receive_drop(struct smb_sb_info *server);
55extern int smb_receive(struct smb_sb_info *server, struct smb_request *req);
56extern int smb_send_request(struct smb_request *req);
57/* inode.c */
58extern struct inode *smb_iget(struct super_block *sb, struct smb_fattr *fattr);
59extern void smb_get_inode_attr(struct inode *inode, struct smb_fattr *fattr);
60extern void smb_set_inode_attr(struct inode *inode, struct smb_fattr *fattr);
61extern void smb_invalidate_inodes(struct smb_sb_info *server);
62extern int smb_revalidate_inode(struct dentry *dentry);
63extern int smb_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
64extern int smb_notify_change(struct dentry *dentry, struct iattr *attr);
65/* file.c */
66extern struct address_space_operations smb_file_aops;
67extern struct file_operations smb_file_operations;
68extern struct inode_operations smb_file_inode_operations;
69/* ioctl.c */
70extern int smb_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
71/* smbiod.c */
72extern void smbiod_wake_up(void);
73extern int smbiod_register_server(struct smb_sb_info *server);
74extern void smbiod_unregister_server(struct smb_sb_info *server);
75extern void smbiod_flush(struct smb_sb_info *server);
76extern int smbiod_retry(struct smb_sb_info *server);
77/* request.c */
78extern int smb_init_request_cache(void);
79extern void smb_destroy_request_cache(void);
80extern struct smb_request *smb_alloc_request(struct smb_sb_info *server, int bufsize);
81extern void smb_rput(struct smb_request *req);
82extern int smb_add_request(struct smb_request *req);
83extern int smb_request_send_server(struct smb_sb_info *server);
84extern int smb_request_recv(struct smb_sb_info *server);
85/* symlink.c */
86extern int smb_symlink(struct inode *inode, struct dentry *dentry, const char *oldname);
87extern struct inode_operations smb_link_inode_operations;
diff --git a/fs/smbfs/request.c b/fs/smbfs/request.c
new file mode 100644
index 000000000000..2d85dd7415bb
--- /dev/null
+++ b/fs/smbfs/request.c
@@ -0,0 +1,823 @@
1/*
2 * request.c
3 *
4 * Copyright (C) 2001 by Urban Widmark
5 *
6 * Please add a note about your changes to smbfs in the ChangeLog file.
7 */
8
9#include <linux/types.h>
10#include <linux/fs.h>
11#include <linux/slab.h>
12#include <linux/net.h>
13
14#include <linux/smb_fs.h>
15#include <linux/smbno.h>
16#include <linux/smb_mount.h>
17
18#include "smb_debug.h"
19#include "request.h"
20#include "proto.h"
21
22/* #define SMB_SLAB_DEBUG (SLAB_RED_ZONE | SLAB_POISON) */
23#define SMB_SLAB_DEBUG 0
24
25#define ROUND_UP(x) (((x)+3) & ~3)
26
27/* cache for request structures */
28static kmem_cache_t *req_cachep;
29
30static int smb_request_send_req(struct smb_request *req);
31
32/*
33 /proc/slabinfo:
34 name, active, num, objsize, active_slabs, num_slaps, #pages
35*/
36
37
38int smb_init_request_cache(void)
39{
40 req_cachep = kmem_cache_create("smb_request",
41 sizeof(struct smb_request), 0,
42 SMB_SLAB_DEBUG | SLAB_HWCACHE_ALIGN,
43 NULL, NULL);
44 if (req_cachep == NULL)
45 return -ENOMEM;
46
47 return 0;
48}
49
50void smb_destroy_request_cache(void)
51{
52 if (kmem_cache_destroy(req_cachep))
53 printk(KERN_INFO "smb_destroy_request_cache: not all structures were freed\n");
54}
55
56/*
57 * Allocate and initialise a request structure
58 */
59static struct smb_request *smb_do_alloc_request(struct smb_sb_info *server,
60 int bufsize)
61{
62 struct smb_request *req;
63 unsigned char *buf = NULL;
64
65 req = kmem_cache_alloc(req_cachep, SLAB_KERNEL);
66 VERBOSE("allocating request: %p\n", req);
67 if (!req)
68 goto out;
69
70 if (bufsize > 0) {
71 buf = smb_kmalloc(bufsize, GFP_NOFS);
72 if (!buf) {
73 kmem_cache_free(req_cachep, req);
74 return NULL;
75 }
76 }
77
78 memset(req, 0, sizeof(struct smb_request));
79 req->rq_buffer = buf;
80 req->rq_bufsize = bufsize;
81 req->rq_server = server;
82 init_waitqueue_head(&req->rq_wait);
83 INIT_LIST_HEAD(&req->rq_queue);
84 atomic_set(&req->rq_count, 1);
85
86out:
87 return req;
88}
89
90struct smb_request *smb_alloc_request(struct smb_sb_info *server, int bufsize)
91{
92 struct smb_request *req = NULL;
93
94 for (;;) {
95 atomic_inc(&server->nr_requests);
96 if (atomic_read(&server->nr_requests) <= MAX_REQUEST_HARD) {
97 req = smb_do_alloc_request(server, bufsize);
98 if (req != NULL)
99 break;
100 }
101
102#if 0
103 /*
104 * Try to free up at least one request in order to stay
105 * below the hard limit
106 */
107 if (nfs_try_to_free_pages(server))
108 continue;
109
110 if (signalled() && (server->flags & NFS_MOUNT_INTR))
111 return ERR_PTR(-ERESTARTSYS);
112 current->policy = SCHED_YIELD;
113 schedule();
114#else
115 /* FIXME: we want something like nfs does above, but that
116 requires changes to all callers and can wait. */
117 break;
118#endif
119 }
120 return req;
121}
122
123static void smb_free_request(struct smb_request *req)
124{
125 atomic_dec(&req->rq_server->nr_requests);
126 if (req->rq_buffer && !(req->rq_flags & SMB_REQ_STATIC))
127 smb_kfree(req->rq_buffer);
128 if (req->rq_trans2buffer)
129 smb_kfree(req->rq_trans2buffer);
130 kmem_cache_free(req_cachep, req);
131}
132
133/*
134 * What prevents a rget to race with a rput? The count must never drop to zero
135 * while it is in use. Only rput if it is ok that it is free'd.
136 */
137static void smb_rget(struct smb_request *req)
138{
139 atomic_inc(&req->rq_count);
140}
141void smb_rput(struct smb_request *req)
142{
143 if (atomic_dec_and_test(&req->rq_count)) {
144 list_del_init(&req->rq_queue);
145 smb_free_request(req);
146 }
147}
148
149/* setup to receive the data part of the SMB */
150static int smb_setup_bcc(struct smb_request *req)
151{
152 int result = 0;
153 req->rq_rlen = smb_len(req->rq_header) + 4 - req->rq_bytes_recvd;
154
155 if (req->rq_rlen > req->rq_bufsize) {
156 PARANOIA("Packet too large %d > %d\n",
157 req->rq_rlen, req->rq_bufsize);
158 return -ENOBUFS;
159 }
160
161 req->rq_iov[0].iov_base = req->rq_buffer;
162 req->rq_iov[0].iov_len = req->rq_rlen;
163 req->rq_iovlen = 1;
164
165 return result;
166}
167
168/*
169 * Prepare a "normal" request structure.
170 */
171static int smb_setup_request(struct smb_request *req)
172{
173 int len = smb_len(req->rq_header) + 4;
174 req->rq_slen = len;
175
176 /* if we expect a data part in the reply we set the iov's to read it */
177 if (req->rq_resp_bcc)
178 req->rq_setup_read = smb_setup_bcc;
179
180 /* This tries to support re-using the same request */
181 req->rq_bytes_sent = 0;
182 req->rq_rcls = 0;
183 req->rq_err = 0;
184 req->rq_errno = 0;
185 req->rq_fragment = 0;
186 if (req->rq_trans2buffer)
187 smb_kfree(req->rq_trans2buffer);
188
189 return 0;
190}
191
192/*
193 * Prepare a transaction2 request structure
194 */
195static int smb_setup_trans2request(struct smb_request *req)
196{
197 struct smb_sb_info *server = req->rq_server;
198 int mparam, mdata;
199 static unsigned char padding[4];
200
201 /* I know the following is very ugly, but I want to build the
202 smb packet as efficiently as possible. */
203
204 const int smb_parameters = 15;
205 const int header = SMB_HEADER_LEN + 2 * smb_parameters + 2;
206 const int oparam = ROUND_UP(header + 3);
207 const int odata = ROUND_UP(oparam + req->rq_lparm);
208 const int bcc = (req->rq_data ? odata + req->rq_ldata :
209 oparam + req->rq_lparm) - header;
210
211 if ((bcc + oparam) > server->opt.max_xmit)
212 return -ENOMEM;
213 smb_setup_header(req, SMBtrans2, smb_parameters, bcc);
214
215 /*
216 * max parameters + max data + max setup == bufsize to make NT4 happy
217 * and not abort the transfer or split into multiple responses. It also
218 * makes smbfs happy as handling packets larger than the buffer size
219 * is extra work.
220 *
221 * OS/2 is probably going to hate me for this ...
222 */
223 mparam = SMB_TRANS2_MAX_PARAM;
224 mdata = req->rq_bufsize - mparam;
225
226 mdata = server->opt.max_xmit - mparam - 100;
227 if (mdata < 1024) {
228 mdata = 1024;
229 mparam = 20;
230 }
231
232#if 0
233 /* NT/win2k has ~4k max_xmit, so with this we request more than it wants
234 to return as one SMB. Useful for testing the fragmented trans2
235 handling. */
236 mdata = 8192;
237#endif
238
239 WSET(req->rq_header, smb_tpscnt, req->rq_lparm);
240 WSET(req->rq_header, smb_tdscnt, req->rq_ldata);
241 WSET(req->rq_header, smb_mprcnt, mparam);
242 WSET(req->rq_header, smb_mdrcnt, mdata);
243 WSET(req->rq_header, smb_msrcnt, 0); /* max setup always 0 ? */
244 WSET(req->rq_header, smb_flags, 0);
245 DSET(req->rq_header, smb_timeout, 0);
246 WSET(req->rq_header, smb_pscnt, req->rq_lparm);
247 WSET(req->rq_header, smb_psoff, oparam - 4);
248 WSET(req->rq_header, smb_dscnt, req->rq_ldata);
249 WSET(req->rq_header, smb_dsoff, req->rq_data ? odata - 4 : 0);
250 *(req->rq_header + smb_suwcnt) = 0x01; /* setup count */
251 *(req->rq_header + smb_suwcnt + 1) = 0x00; /* reserved */
252 WSET(req->rq_header, smb_setup0, req->rq_trans2_command);
253
254 req->rq_iovlen = 2;
255 req->rq_iov[0].iov_base = (void *) req->rq_header;
256 req->rq_iov[0].iov_len = oparam;
257 req->rq_iov[1].iov_base = (req->rq_parm==NULL) ? padding : req->rq_parm;
258 req->rq_iov[1].iov_len = req->rq_lparm;
259 req->rq_slen = oparam + req->rq_lparm;
260
261 if (req->rq_data) {
262 req->rq_iovlen += 2;
263 req->rq_iov[2].iov_base = padding;
264 req->rq_iov[2].iov_len = odata - oparam - req->rq_lparm;
265 req->rq_iov[3].iov_base = req->rq_data;
266 req->rq_iov[3].iov_len = req->rq_ldata;
267 req->rq_slen = odata + req->rq_ldata;
268 }
269
270 /* always a data part for trans2 replies */
271 req->rq_setup_read = smb_setup_bcc;
272
273 return 0;
274}
275
276/*
277 * Add a request and tell smbiod to process it
278 */
279int smb_add_request(struct smb_request *req)
280{
281 long timeleft;
282 struct smb_sb_info *server = req->rq_server;
283 int result = 0;
284
285 smb_setup_request(req);
286 if (req->rq_trans2_command) {
287 if (req->rq_buffer == NULL) {
288 PARANOIA("trans2 attempted without response buffer!\n");
289 return -EIO;
290 }
291 result = smb_setup_trans2request(req);
292 }
293 if (result < 0)
294 return result;
295
296#ifdef SMB_DEBUG_PACKET_SIZE
297 add_xmit_stats(req);
298#endif
299
300 /* add 'req' to the queue of requests */
301 if (smb_lock_server_interruptible(server))
302 return -EINTR;
303
304 /*
305 * Try to send the request as the process. If that fails we queue the
306 * request and let smbiod send it later.
307 */
308
309 /* FIXME: each server has a number on the maximum number of parallel
310 requests. 10, 50 or so. We should not allow more requests to be
311 active. */
312 if (server->mid > 0xf000)
313 server->mid = 0;
314 req->rq_mid = server->mid++;
315 WSET(req->rq_header, smb_mid, req->rq_mid);
316
317 result = 0;
318 if (server->state == CONN_VALID) {
319 if (list_empty(&server->xmitq))
320 result = smb_request_send_req(req);
321 if (result < 0) {
322 /* Connection lost? */
323 server->conn_error = result;
324 server->state = CONN_INVALID;
325 }
326 }
327 if (result != 1)
328 list_add_tail(&req->rq_queue, &server->xmitq);
329 smb_rget(req);
330
331 if (server->state != CONN_VALID)
332 smbiod_retry(server);
333
334 smb_unlock_server(server);
335
336 smbiod_wake_up();
337
338 timeleft = wait_event_interruptible_timeout(req->rq_wait,
339 req->rq_flags & SMB_REQ_RECEIVED, 30*HZ);
340 if (!timeleft || signal_pending(current)) {
341 /*
342 * On timeout or on interrupt we want to try and remove the
343 * request from the recvq/xmitq.
344 */
345 smb_lock_server(server);
346 if (!(req->rq_flags & SMB_REQ_RECEIVED)) {
347 list_del_init(&req->rq_queue);
348 smb_rput(req);
349 }
350 smb_unlock_server(server);
351 }
352
353 if (!timeleft) {
354 PARANOIA("request [%p, mid=%d] timed out!\n",
355 req, req->rq_mid);
356 VERBOSE("smb_com: %02x\n", *(req->rq_header + smb_com));
357 VERBOSE("smb_rcls: %02x\n", *(req->rq_header + smb_rcls));
358 VERBOSE("smb_flg: %02x\n", *(req->rq_header + smb_flg));
359 VERBOSE("smb_tid: %04x\n", WVAL(req->rq_header, smb_tid));
360 VERBOSE("smb_pid: %04x\n", WVAL(req->rq_header, smb_pid));
361 VERBOSE("smb_uid: %04x\n", WVAL(req->rq_header, smb_uid));
362 VERBOSE("smb_mid: %04x\n", WVAL(req->rq_header, smb_mid));
363 VERBOSE("smb_wct: %02x\n", *(req->rq_header + smb_wct));
364
365 req->rq_rcls = ERRSRV;
366 req->rq_err = ERRtimeout;
367
368 /* Just in case it was "stuck" */
369 smbiod_wake_up();
370 }
371 VERBOSE("woke up, rcls=%d\n", req->rq_rcls);
372
373 if (req->rq_rcls != 0)
374 req->rq_errno = smb_errno(req);
375 if (signal_pending(current))
376 req->rq_errno = -ERESTARTSYS;
377 return req->rq_errno;
378}
379
380/*
381 * Send a request and place it on the recvq if successfully sent.
382 * Must be called with the server lock held.
383 */
384static int smb_request_send_req(struct smb_request *req)
385{
386 struct smb_sb_info *server = req->rq_server;
387 int result;
388
389 if (req->rq_bytes_sent == 0) {
390 WSET(req->rq_header, smb_tid, server->opt.tid);
391 WSET(req->rq_header, smb_pid, 1);
392 WSET(req->rq_header, smb_uid, server->opt.server_uid);
393 }
394
395 result = smb_send_request(req);
396 if (result < 0 && result != -EAGAIN)
397 goto out;
398
399 result = 0;
400 if (!(req->rq_flags & SMB_REQ_TRANSMITTED))
401 goto out;
402
403 list_del_init(&req->rq_queue);
404 list_add_tail(&req->rq_queue, &server->recvq);
405 result = 1;
406out:
407 return result;
408}
409
410/*
411 * Sends one request for this server. (smbiod)
412 * Must be called with the server lock held.
413 * Returns: <0 on error
414 * 0 if no request could be completely sent
415 * 1 if all data for one request was sent
416 */
417int smb_request_send_server(struct smb_sb_info *server)
418{
419 struct list_head *head;
420 struct smb_request *req;
421 int result;
422
423 if (server->state != CONN_VALID)
424 return 0;
425
426 /* dequeue first request, if any */
427 req = NULL;
428 head = server->xmitq.next;
429 if (head != &server->xmitq) {
430 req = list_entry(head, struct smb_request, rq_queue);
431 }
432 if (!req)
433 return 0;
434
435 result = smb_request_send_req(req);
436 if (result < 0) {
437 server->conn_error = result;
438 list_del_init(&req->rq_queue);
439 list_add(&req->rq_queue, &server->xmitq);
440 result = -EIO;
441 goto out;
442 }
443
444out:
445 return result;
446}
447
448/*
449 * Try to find a request matching this "mid". Typically the first entry will
450 * be the matching one.
451 */
452static struct smb_request *find_request(struct smb_sb_info *server, int mid)
453{
454 struct list_head *tmp;
455 struct smb_request *req = NULL;
456
457 list_for_each(tmp, &server->recvq) {
458 req = list_entry(tmp, struct smb_request, rq_queue);
459 if (req->rq_mid == mid) {
460 break;
461 }
462 req = NULL;
463 }
464
465 if (!req) {
466 VERBOSE("received reply with mid %d but no request!\n",
467 WVAL(server->header, smb_mid));
468 server->rstate = SMB_RECV_DROP;
469 }
470
471 return req;
472}
473
474/*
475 * Called when we have read the smb header and believe this is a response.
476 */
477static int smb_init_request(struct smb_sb_info *server, struct smb_request *req)
478{
479 int hdrlen, wct;
480
481 memcpy(req->rq_header, server->header, SMB_HEADER_LEN);
482
483 wct = *(req->rq_header + smb_wct);
484 if (wct > 20) {
485 PARANOIA("wct too large, %d > 20\n", wct);
486 server->rstate = SMB_RECV_DROP;
487 return 0;
488 }
489
490 req->rq_resp_wct = wct;
491 hdrlen = SMB_HEADER_LEN + wct*2 + 2;
492 VERBOSE("header length: %d smb_wct: %2d\n", hdrlen, wct);
493
494 req->rq_bytes_recvd = SMB_HEADER_LEN;
495 req->rq_rlen = hdrlen;
496 req->rq_iov[0].iov_base = req->rq_header;
497 req->rq_iov[0].iov_len = hdrlen;
498 req->rq_iovlen = 1;
499 server->rstate = SMB_RECV_PARAM;
500
501#ifdef SMB_DEBUG_PACKET_SIZE
502 add_recv_stats(smb_len(server->header));
503#endif
504 return 0;
505}
506
507/*
508 * Reads the SMB parameters
509 */
510static int smb_recv_param(struct smb_sb_info *server, struct smb_request *req)
511{
512 int result;
513
514 result = smb_receive(server, req);
515 if (result < 0)
516 return result;
517 if (req->rq_bytes_recvd < req->rq_rlen)
518 return 0;
519
520 VERBOSE("result: %d smb_bcc: %04x\n", result,
521 WVAL(req->rq_header, SMB_HEADER_LEN +
522 (*(req->rq_header + smb_wct) * 2)));
523
524 result = 0;
525 req->rq_iov[0].iov_base = NULL;
526 req->rq_rlen = 0;
527 if (req->rq_callback)
528 req->rq_callback(req);
529 else if (req->rq_setup_read)
530 result = req->rq_setup_read(req);
531 if (result < 0) {
532 server->rstate = SMB_RECV_DROP;
533 return result;
534 }
535
536 server->rstate = req->rq_rlen > 0 ? SMB_RECV_DATA : SMB_RECV_END;
537
538 req->rq_bytes_recvd = 0; // recvd out of the iov
539
540 VERBOSE("rlen: %d\n", req->rq_rlen);
541 if (req->rq_rlen < 0) {
542 PARANOIA("Parameters read beyond end of packet!\n");
543 server->rstate = SMB_RECV_END;
544 return -EIO;
545 }
546 return 0;
547}
548
549/*
550 * Reads the SMB data
551 */
552static int smb_recv_data(struct smb_sb_info *server, struct smb_request *req)
553{
554 int result;
555
556 result = smb_receive(server, req);
557 if (result < 0)
558 goto out;
559 if (req->rq_bytes_recvd < req->rq_rlen)
560 goto out;
561 server->rstate = SMB_RECV_END;
562out:
563 VERBOSE("result: %d\n", result);
564 return result;
565}
566
567/*
568 * Receive a transaction2 response
569 * Return: 0 if the response has been fully read
570 * 1 if there are further "fragments" to read
571 * <0 if there is an error
572 */
573static int smb_recv_trans2(struct smb_sb_info *server, struct smb_request *req)
574{
575 unsigned char *inbuf;
576 unsigned int parm_disp, parm_offset, parm_count, parm_tot;
577 unsigned int data_disp, data_offset, data_count, data_tot;
578 int hdrlen = SMB_HEADER_LEN + req->rq_resp_wct*2 - 2;
579
580 VERBOSE("handling trans2\n");
581
582 inbuf = req->rq_header;
583 data_tot = WVAL(inbuf, smb_tdrcnt);
584 parm_tot = WVAL(inbuf, smb_tprcnt);
585 parm_disp = WVAL(inbuf, smb_prdisp);
586 parm_offset = WVAL(inbuf, smb_proff);
587 parm_count = WVAL(inbuf, smb_prcnt);
588 data_disp = WVAL(inbuf, smb_drdisp);
589 data_offset = WVAL(inbuf, smb_droff);
590 data_count = WVAL(inbuf, smb_drcnt);
591
592 /* Modify offset for the split header/buffer we use */
593 if (data_count || data_offset) {
594 if (unlikely(data_offset < hdrlen))
595 goto out_bad_data;
596 else
597 data_offset -= hdrlen;
598 }
599 if (parm_count || parm_offset) {
600 if (unlikely(parm_offset < hdrlen))
601 goto out_bad_parm;
602 else
603 parm_offset -= hdrlen;
604 }
605
606 if (parm_count == parm_tot && data_count == data_tot) {
607 /*
608 * This packet has all the trans2 data.
609 *
610 * We setup the request so that this will be the common
611 * case. It may be a server error to not return a
612 * response that fits.
613 */
614 VERBOSE("single trans2 response "
615 "dcnt=%u, pcnt=%u, doff=%u, poff=%u\n",
616 data_count, parm_count,
617 data_offset, parm_offset);
618 req->rq_ldata = data_count;
619 req->rq_lparm = parm_count;
620 req->rq_data = req->rq_buffer + data_offset;
621 req->rq_parm = req->rq_buffer + parm_offset;
622 if (unlikely(parm_offset + parm_count > req->rq_rlen))
623 goto out_bad_parm;
624 if (unlikely(data_offset + data_count > req->rq_rlen))
625 goto out_bad_data;
626 return 0;
627 }
628
629 VERBOSE("multi trans2 response "
630 "frag=%d, dcnt=%u, pcnt=%u, doff=%u, poff=%u\n",
631 req->rq_fragment,
632 data_count, parm_count,
633 data_offset, parm_offset);
634
635 if (!req->rq_fragment) {
636 int buf_len;
637
638 /* We got the first trans2 fragment */
639 req->rq_fragment = 1;
640 req->rq_total_data = data_tot;
641 req->rq_total_parm = parm_tot;
642 req->rq_ldata = 0;
643 req->rq_lparm = 0;
644
645 buf_len = data_tot + parm_tot;
646 if (buf_len > SMB_MAX_PACKET_SIZE)
647 goto out_too_long;
648
649 req->rq_trans2bufsize = buf_len;
650 req->rq_trans2buffer = smb_kmalloc(buf_len, GFP_NOFS);
651 if (!req->rq_trans2buffer)
652 goto out_no_mem;
653 memset(req->rq_trans2buffer, 0, buf_len);
654
655 req->rq_parm = req->rq_trans2buffer;
656 req->rq_data = req->rq_trans2buffer + parm_tot;
657 } else if (unlikely(req->rq_total_data < data_tot ||
658 req->rq_total_parm < parm_tot))
659 goto out_data_grew;
660
661 if (unlikely(parm_disp + parm_count > req->rq_total_parm ||
662 parm_offset + parm_count > req->rq_rlen))
663 goto out_bad_parm;
664 if (unlikely(data_disp + data_count > req->rq_total_data ||
665 data_offset + data_count > req->rq_rlen))
666 goto out_bad_data;
667
668 inbuf = req->rq_buffer;
669 memcpy(req->rq_parm + parm_disp, inbuf + parm_offset, parm_count);
670 memcpy(req->rq_data + data_disp, inbuf + data_offset, data_count);
671
672 req->rq_ldata += data_count;
673 req->rq_lparm += parm_count;
674
675 /*
676 * Check whether we've received all of the data. Note that
677 * we use the packet totals -- total lengths might shrink!
678 */
679 if (req->rq_ldata >= data_tot && req->rq_lparm >= parm_tot) {
680 req->rq_ldata = data_tot;
681 req->rq_lparm = parm_tot;
682 return 0;
683 }
684 return 1;
685
686out_too_long:
687 printk(KERN_ERR "smb_trans2: data/param too long, data=%u, parm=%u\n",
688 data_tot, parm_tot);
689 goto out_EIO;
690out_no_mem:
691 printk(KERN_ERR "smb_trans2: couldn't allocate data area of %d bytes\n",
692 req->rq_trans2bufsize);
693 req->rq_errno = -ENOMEM;
694 goto out;
695out_data_grew:
696 printk(KERN_ERR "smb_trans2: data/params grew!\n");
697 goto out_EIO;
698out_bad_parm:
699 printk(KERN_ERR "smb_trans2: invalid parms, disp=%u, cnt=%u, tot=%u, ofs=%u\n",
700 parm_disp, parm_count, parm_tot, parm_offset);
701 goto out_EIO;
702out_bad_data:
703 printk(KERN_ERR "smb_trans2: invalid data, disp=%u, cnt=%u, tot=%u, ofs=%u\n",
704 data_disp, data_count, data_tot, data_offset);
705out_EIO:
706 req->rq_errno = -EIO;
707out:
708 return req->rq_errno;
709}
710
711/*
712 * State machine for receiving responses. We handle the fact that we can't
713 * read the full response in one try by having states telling us how much we
714 * have read.
715 *
716 * Must be called with the server lock held (only called from smbiod).
717 *
718 * Return: <0 on error
719 */
720int smb_request_recv(struct smb_sb_info *server)
721{
722 struct smb_request *req = NULL;
723 int result = 0;
724
725 if (smb_recv_available(server) <= 0)
726 return 0;
727
728 VERBOSE("state: %d\n", server->rstate);
729 switch (server->rstate) {
730 case SMB_RECV_DROP:
731 result = smb_receive_drop(server);
732 if (result < 0)
733 break;
734 if (server->rstate == SMB_RECV_DROP)
735 break;
736 server->rstate = SMB_RECV_START;
737 /* fallthrough */
738 case SMB_RECV_START:
739 server->smb_read = 0;
740 server->rstate = SMB_RECV_HEADER;
741 /* fallthrough */
742 case SMB_RECV_HEADER:
743 result = smb_receive_header(server);
744 if (result < 0)
745 break;
746 if (server->rstate == SMB_RECV_HEADER)
747 break;
748 if (! (*(server->header + smb_flg) & SMB_FLAGS_REPLY) ) {
749 server->rstate = SMB_RECV_REQUEST;
750 break;
751 }
752 if (server->rstate != SMB_RECV_HCOMPLETE)
753 break;
754 /* fallthrough */
755 case SMB_RECV_HCOMPLETE:
756 req = find_request(server, WVAL(server->header, smb_mid));
757 if (!req)
758 break;
759 smb_init_request(server, req);
760 req->rq_rcls = *(req->rq_header + smb_rcls);
761 req->rq_err = WVAL(req->rq_header, smb_err);
762 if (server->rstate != SMB_RECV_PARAM)
763 break;
764 /* fallthrough */
765 case SMB_RECV_PARAM:
766 if (!req)
767 req = find_request(server,WVAL(server->header,smb_mid));
768 if (!req)
769 break;
770 result = smb_recv_param(server, req);
771 if (result < 0)
772 break;
773 if (server->rstate != SMB_RECV_DATA)
774 break;
775 /* fallthrough */
776 case SMB_RECV_DATA:
777 if (!req)
778 req = find_request(server,WVAL(server->header,smb_mid));
779 if (!req)
780 break;
781 result = smb_recv_data(server, req);
782 if (result < 0)
783 break;
784 break;
785
786 /* We should never be called with any of these states */
787 case SMB_RECV_END:
788 case SMB_RECV_REQUEST:
789 server->rstate = SMB_RECV_END;
790 break;
791 }
792
793 if (result < 0) {
794 /* We saw an error */
795 return result;
796 }
797
798 if (server->rstate != SMB_RECV_END)
799 return 0;
800
801 result = 0;
802 if (req->rq_trans2_command && req->rq_rcls == SUCCESS)
803 result = smb_recv_trans2(server, req);
804
805 /*
806 * Response completely read. Drop any extra bytes sent by the server.
807 * (Yes, servers sometimes add extra bytes to responses)
808 */
809 VERBOSE("smb_len: %d smb_read: %d\n",
810 server->smb_len, server->smb_read);
811 if (server->smb_read < server->smb_len)
812 smb_receive_drop(server);
813
814 server->rstate = SMB_RECV_START;
815
816 if (!result) {
817 list_del_init(&req->rq_queue);
818 req->rq_flags |= SMB_REQ_RECEIVED;
819 smb_rput(req);
820 wake_up_interruptible(&req->rq_wait);
821 }
822 return 0;
823}
diff --git a/fs/smbfs/request.h b/fs/smbfs/request.h
new file mode 100644
index 000000000000..efb21451e7c9
--- /dev/null
+++ b/fs/smbfs/request.h
@@ -0,0 +1,70 @@
1#include <linux/list.h>
2#include <linux/types.h>
3#include <linux/uio.h>
4#include <linux/wait.h>
5
6struct smb_request {
7 struct list_head rq_queue; /* recvq or xmitq for the server */
8
9 atomic_t rq_count;
10
11 wait_queue_head_t rq_wait;
12 int rq_flags;
13 int rq_mid; /* multiplex ID, set by request.c */
14
15 struct smb_sb_info *rq_server;
16
17 /* header + word count + parameter words + byte count */
18 unsigned char rq_header[SMB_HEADER_LEN + 20*2 + 2];
19
20 int rq_bufsize;
21 unsigned char *rq_buffer;
22
23 /* FIXME: this is not good enough for merging IO requests. */
24 unsigned char *rq_page;
25 int rq_rsize;
26
27 int rq_resp_wct;
28 int rq_resp_bcc;
29
30 int rq_rlen;
31 int rq_bytes_recvd;
32
33 int rq_slen;
34 int rq_bytes_sent;
35
36 int rq_iovlen;
37 struct kvec rq_iov[4];
38
39 int (*rq_setup_read) (struct smb_request *);
40 void (*rq_callback) (struct smb_request *);
41
42 /* ------ trans2 stuff ------ */
43
44 u16 rq_trans2_command; /* 0 if not a trans2 request */
45 unsigned int rq_ldata;
46 unsigned char *rq_data;
47 unsigned int rq_lparm;
48 unsigned char *rq_parm;
49
50 int rq_fragment;
51 u32 rq_total_data;
52 u32 rq_total_parm;
53 int rq_trans2bufsize;
54 unsigned char *rq_trans2buffer;
55
56 /* ------ response ------ */
57
58 unsigned short rq_rcls;
59 unsigned short rq_err;
60 int rq_errno;
61};
62
63#define SMB_REQ_STATIC 0x0001 /* rq_buffer is static */
64#define SMB_REQ_NORETRY 0x0002 /* request is invalid after retry */
65
66#define SMB_REQ_TRANSMITTED 0x4000 /* all data has been sent */
67#define SMB_REQ_RECEIVED 0x8000 /* reply received, smbiod is done */
68
69#define xSMB_REQ_NOREPLY 0x0004 /* we don't want the reply (if any) */
70#define xSMB_REQ_NORECEIVER 0x0008 /* caller doesn't wait for response */
diff --git a/fs/smbfs/smb_debug.h b/fs/smbfs/smb_debug.h
new file mode 100644
index 000000000000..734972b92694
--- /dev/null
+++ b/fs/smbfs/smb_debug.h
@@ -0,0 +1,34 @@
1/*
2 * Defines some debug macros for smbfs.
3 */
4
5/* This makes a dentry parent/child name pair. Useful for debugging printk's */
6#define DENTRY_PATH(dentry) \
7 (dentry)->d_parent->d_name.name,(dentry)->d_name.name
8
9/*
10 * safety checks that should never happen ???
11 * these are normally enabled.
12 */
13#ifdef SMBFS_PARANOIA
14# define PARANOIA(f, a...) printk(KERN_NOTICE "%s: " f, __FUNCTION__ , ## a)
15#else
16# define PARANOIA(f, a...) do { ; } while(0)
17#endif
18
19/* lots of debug messages */
20#ifdef SMBFS_DEBUG_VERBOSE
21# define VERBOSE(f, a...) printk(KERN_DEBUG "%s: " f, __FUNCTION__ , ## a)
22#else
23# define VERBOSE(f, a...) do { ; } while(0)
24#endif
25
26/*
27 * "normal" debug messages, but not with a normal DEBUG define ... way
28 * too common name.
29 */
30#ifdef SMBFS_DEBUG
31#define DEBUG1(f, a...) printk(KERN_DEBUG "%s: " f, __FUNCTION__ , ## a)
32#else
33#define DEBUG1(f, a...) do { ; } while(0)
34#endif
diff --git a/fs/smbfs/smbiod.c b/fs/smbfs/smbiod.c
new file mode 100644
index 000000000000..481a97a423fa
--- /dev/null
+++ b/fs/smbfs/smbiod.c
@@ -0,0 +1,341 @@
1/*
2 * smbiod.c
3 *
4 * Copyright (C) 2000, Charles Loep / Corel Corp.
5 * Copyright (C) 2001, Urban Widmark
6 */
7
8#include <linux/config.h>
9
10#include <linux/sched.h>
11#include <linux/kernel.h>
12#include <linux/mm.h>
13#include <linux/string.h>
14#include <linux/stat.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/file.h>
19#include <linux/dcache.h>
20#include <linux/smp_lock.h>
21#include <linux/module.h>
22#include <linux/net.h>
23#include <net/ip.h>
24
25#include <linux/smb_fs.h>
26#include <linux/smbno.h>
27#include <linux/smb_mount.h>
28
29#include <asm/system.h>
30#include <asm/uaccess.h>
31
32#include "smb_debug.h"
33#include "request.h"
34#include "proto.h"
35
36enum smbiod_state {
37 SMBIOD_DEAD,
38 SMBIOD_STARTING,
39 SMBIOD_RUNNING,
40};
41
42static enum smbiod_state smbiod_state = SMBIOD_DEAD;
43static pid_t smbiod_pid;
44static DECLARE_WAIT_QUEUE_HEAD(smbiod_wait);
45static LIST_HEAD(smb_servers);
46static DEFINE_SPINLOCK(servers_lock);
47
48#define SMBIOD_DATA_READY (1<<0)
49static long smbiod_flags;
50
51static int smbiod(void *);
52static int smbiod_start(void);
53
54/*
55 * called when there's work for us to do
56 */
57void smbiod_wake_up(void)
58{
59 if (smbiod_state == SMBIOD_DEAD)
60 return;
61 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
62 wake_up_interruptible(&smbiod_wait);
63}
64
65/*
66 * start smbiod if none is running
67 */
68static int smbiod_start(void)
69{
70 pid_t pid;
71 if (smbiod_state != SMBIOD_DEAD)
72 return 0;
73 smbiod_state = SMBIOD_STARTING;
74 __module_get(THIS_MODULE);
75 spin_unlock(&servers_lock);
76 pid = kernel_thread(smbiod, NULL, 0);
77 if (pid < 0)
78 module_put(THIS_MODULE);
79
80 spin_lock(&servers_lock);
81 smbiod_state = pid < 0 ? SMBIOD_DEAD : SMBIOD_RUNNING;
82 smbiod_pid = pid;
83 return pid;
84}
85
86/*
87 * register a server & start smbiod if necessary
88 */
89int smbiod_register_server(struct smb_sb_info *server)
90{
91 int ret;
92 spin_lock(&servers_lock);
93 list_add(&server->entry, &smb_servers);
94 VERBOSE("%p\n", server);
95 ret = smbiod_start();
96 spin_unlock(&servers_lock);
97 return ret;
98}
99
100/*
101 * Unregister a server
102 * Must be called with the server lock held.
103 */
104void smbiod_unregister_server(struct smb_sb_info *server)
105{
106 spin_lock(&servers_lock);
107 list_del_init(&server->entry);
108 VERBOSE("%p\n", server);
109 spin_unlock(&servers_lock);
110
111 smbiod_wake_up();
112 smbiod_flush(server);
113}
114
115void smbiod_flush(struct smb_sb_info *server)
116{
117 struct list_head *tmp, *n;
118 struct smb_request *req;
119
120 list_for_each_safe(tmp, n, &server->xmitq) {
121 req = list_entry(tmp, struct smb_request, rq_queue);
122 req->rq_errno = -EIO;
123 list_del_init(&req->rq_queue);
124 smb_rput(req);
125 wake_up_interruptible(&req->rq_wait);
126 }
127 list_for_each_safe(tmp, n, &server->recvq) {
128 req = list_entry(tmp, struct smb_request, rq_queue);
129 req->rq_errno = -EIO;
130 list_del_init(&req->rq_queue);
131 smb_rput(req);
132 wake_up_interruptible(&req->rq_wait);
133 }
134}
135
136/*
137 * Wake up smbmount and make it reconnect to the server.
138 * This must be called with the server locked.
139 *
140 * FIXME: add smbconnect version to this
141 */
142int smbiod_retry(struct smb_sb_info *server)
143{
144 struct list_head *head;
145 struct smb_request *req;
146 pid_t pid = server->conn_pid;
147 int result = 0;
148
149 VERBOSE("state: %d\n", server->state);
150 if (server->state == CONN_VALID || server->state == CONN_RETRYING)
151 goto out;
152
153 smb_invalidate_inodes(server);
154
155 /*
156 * Some requests are meaningless after a retry, so we abort them.
157 * One example are all requests using 'fileid' since the files are
158 * closed on retry.
159 */
160 head = server->xmitq.next;
161 while (head != &server->xmitq) {
162 req = list_entry(head, struct smb_request, rq_queue);
163 head = head->next;
164
165 req->rq_bytes_sent = 0;
166 if (req->rq_flags & SMB_REQ_NORETRY) {
167 VERBOSE("aborting request %p on xmitq\n", req);
168 req->rq_errno = -EIO;
169 list_del_init(&req->rq_queue);
170 smb_rput(req);
171 wake_up_interruptible(&req->rq_wait);
172 }
173 }
174
175 /*
176 * FIXME: test the code for retrying request we already sent
177 */
178 head = server->recvq.next;
179 while (head != &server->recvq) {
180 req = list_entry(head, struct smb_request, rq_queue);
181 head = head->next;
182#if 0
183 if (req->rq_flags & SMB_REQ_RETRY) {
184 /* must move the request to the xmitq */
185 VERBOSE("retrying request %p on recvq\n", req);
186 list_del(&req->rq_queue);
187 list_add(&req->rq_queue, &server->xmitq);
188 continue;
189 }
190#endif
191
192 VERBOSE("aborting request %p on recvq\n", req);
193 /* req->rq_rcls = ???; */ /* FIXME: set smb error code too? */
194 req->rq_errno = -EIO;
195 list_del_init(&req->rq_queue);
196 smb_rput(req);
197 wake_up_interruptible(&req->rq_wait);
198 }
199
200 smb_close_socket(server);
201
202 if (pid == 0) {
203 /* FIXME: this is fatal, umount? */
204 printk(KERN_ERR "smb_retry: no connection process\n");
205 server->state = CONN_RETRIED;
206 goto out;
207 }
208
209 /*
210 * Change state so that only one retry per server will be started.
211 */
212 server->state = CONN_RETRYING;
213
214 /*
215 * Note: use the "priv" flag, as a user process may need to reconnect.
216 */
217 result = kill_proc(pid, SIGUSR1, 1);
218 if (result) {
219 /* FIXME: this is most likely fatal, umount? */
220 printk(KERN_ERR "smb_retry: signal failed [%d]\n", result);
221 goto out;
222 }
223 VERBOSE("signalled pid %d\n", pid);
224
225 /* FIXME: The retried requests should perhaps get a "time boost". */
226
227out:
228 return result;
229}
230
231/*
232 * Currently handles lockingX packets.
233 */
234static void smbiod_handle_request(struct smb_sb_info *server)
235{
236 PARANOIA("smbiod got a request ... and we don't implement oplocks!\n");
237 server->rstate = SMB_RECV_DROP;
238}
239
240/*
241 * Do some IO for one server.
242 */
243static void smbiod_doio(struct smb_sb_info *server)
244{
245 int result;
246 int maxwork = 7;
247
248 if (server->state != CONN_VALID)
249 goto out;
250
251 do {
252 result = smb_request_recv(server);
253 if (result < 0) {
254 server->state = CONN_INVALID;
255 smbiod_retry(server);
256 goto out; /* reconnecting is slow */
257 } else if (server->rstate == SMB_RECV_REQUEST)
258 smbiod_handle_request(server);
259 } while (result > 0 && maxwork-- > 0);
260
261 /*
262 * If there is more to read then we want to be sure to wake up again.
263 */
264 if (server->state != CONN_VALID)
265 goto out;
266 if (smb_recv_available(server) > 0)
267 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
268
269 do {
270 result = smb_request_send_server(server);
271 if (result < 0) {
272 server->state = CONN_INVALID;
273 smbiod_retry(server);
274 goto out; /* reconnecting is slow */
275 }
276 } while (result > 0);
277
278 /*
279 * If the last request was not sent out we want to wake up again.
280 */
281 if (!list_empty(&server->xmitq))
282 set_bit(SMBIOD_DATA_READY, &smbiod_flags);
283
284out:
285 return;
286}
287
288/*
289 * smbiod kernel thread
290 */
291static int smbiod(void *unused)
292{
293 daemonize("smbiod");
294
295 allow_signal(SIGKILL);
296
297 VERBOSE("SMB Kernel thread starting (%d) ...\n", current->pid);
298
299 for (;;) {
300 struct smb_sb_info *server;
301 struct list_head *pos, *n;
302
303 /* FIXME: Use poll? */
304 wait_event_interruptible(smbiod_wait,
305 test_bit(SMBIOD_DATA_READY, &smbiod_flags));
306 if (signal_pending(current)) {
307 spin_lock(&servers_lock);
308 smbiod_state = SMBIOD_DEAD;
309 spin_unlock(&servers_lock);
310 break;
311 }
312
313 clear_bit(SMBIOD_DATA_READY, &smbiod_flags);
314
315 spin_lock(&servers_lock);
316 if (list_empty(&smb_servers)) {
317 smbiod_state = SMBIOD_DEAD;
318 spin_unlock(&servers_lock);
319 break;
320 }
321
322 list_for_each_safe(pos, n, &smb_servers) {
323 server = list_entry(pos, struct smb_sb_info, entry);
324 VERBOSE("checking server %p\n", server);
325
326 if (server->state == CONN_VALID) {
327 spin_unlock(&servers_lock);
328
329 smb_lock_server(server);
330 smbiod_doio(server);
331 smb_unlock_server(server);
332
333 spin_lock(&servers_lock);
334 }
335 }
336 spin_unlock(&servers_lock);
337 }
338
339 VERBOSE("SMB Kernel thread exiting (%d) ...\n", current->pid);
340 module_put_and_exit(0);
341}
diff --git a/fs/smbfs/sock.c b/fs/smbfs/sock.c
new file mode 100644
index 000000000000..93f3cd22a2e9
--- /dev/null
+++ b/fs/smbfs/sock.c
@@ -0,0 +1,388 @@
1/*
2 * sock.c
3 *
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/fs.h>
11#include <linux/time.h>
12#include <linux/errno.h>
13#include <linux/socket.h>
14#include <linux/fcntl.h>
15#include <linux/file.h>
16#include <linux/in.h>
17#include <linux/net.h>
18#include <linux/tcp.h>
19#include <linux/mm.h>
20#include <linux/netdevice.h>
21#include <linux/smp_lock.h>
22#include <linux/workqueue.h>
23#include <net/scm.h>
24#include <net/ip.h>
25
26#include <linux/smb_fs.h>
27#include <linux/smb.h>
28#include <linux/smbno.h>
29
30#include <asm/uaccess.h>
31#include <asm/ioctls.h>
32
33#include "smb_debug.h"
34#include "proto.h"
35#include "request.h"
36
37
38static int
39_recvfrom(struct socket *socket, unsigned char *ubuf, int size, unsigned flags)
40{
41 struct kvec iov = {ubuf, size};
42 struct msghdr msg = {.msg_flags = flags};
43 msg.msg_flags |= MSG_DONTWAIT | MSG_NOSIGNAL;
44 return kernel_recvmsg(socket, &msg, &iov, 1, size, msg.msg_flags);
45}
46
47/*
48 * Return the server this socket belongs to
49 */
50static struct smb_sb_info *
51server_from_socket(struct socket *socket)
52{
53 return socket->sk->sk_user_data;
54}
55
56/*
57 * Called when there is data on the socket.
58 */
59void
60smb_data_ready(struct sock *sk, int len)
61{
62 struct smb_sb_info *server = server_from_socket(sk->sk_socket);
63 void (*data_ready)(struct sock *, int) = server->data_ready;
64
65 data_ready(sk, len);
66 VERBOSE("(%p, %d)\n", sk, len);
67 smbiod_wake_up();
68}
69
70int
71smb_valid_socket(struct inode * inode)
72{
73 return (inode && S_ISSOCK(inode->i_mode) &&
74 SOCKET_I(inode)->type == SOCK_STREAM);
75}
76
77static struct socket *
78server_sock(struct smb_sb_info *server)
79{
80 struct file *file;
81
82 if (server && (file = server->sock_file))
83 {
84#ifdef SMBFS_PARANOIA
85 if (!smb_valid_socket(file->f_dentry->d_inode))
86 PARANOIA("bad socket!\n");
87#endif
88 return SOCKET_I(file->f_dentry->d_inode);
89 }
90 return NULL;
91}
92
93void
94smb_close_socket(struct smb_sb_info *server)
95{
96 struct file * file = server->sock_file;
97
98 if (file) {
99 struct socket *sock = server_sock(server);
100
101 VERBOSE("closing socket %p\n", sock);
102 sock->sk->sk_data_ready = server->data_ready;
103 server->sock_file = NULL;
104 fput(file);
105 }
106}
107
108static int
109smb_get_length(struct socket *socket, unsigned char *header)
110{
111 int result;
112
113 result = _recvfrom(socket, header, 4, MSG_PEEK);
114 if (result == -EAGAIN)
115 return -ENODATA;
116 if (result < 0) {
117 PARANOIA("recv error = %d\n", -result);
118 return result;
119 }
120 if (result < 4)
121 return -ENODATA;
122
123 switch (header[0]) {
124 case 0x00:
125 case 0x82:
126 break;
127
128 case 0x85:
129 DEBUG1("Got SESSION KEEP ALIVE\n");
130 _recvfrom(socket, header, 4, 0); /* read away */
131 return -ENODATA;
132
133 default:
134 PARANOIA("Invalid NBT packet, code=%x\n", header[0]);
135 return -EIO;
136 }
137
138 /* The length in the RFC NB header is the raw data length */
139 return smb_len(header);
140}
141
142int
143smb_recv_available(struct smb_sb_info *server)
144{
145 mm_segment_t oldfs;
146 int avail, err;
147 struct socket *sock = server_sock(server);
148
149 oldfs = get_fs();
150 set_fs(get_ds());
151 err = sock->ops->ioctl(sock, SIOCINQ, (unsigned long) &avail);
152 set_fs(oldfs);
153 return (err >= 0) ? avail : err;
154}
155
156/*
157 * Adjust the kvec to move on 'n' bytes (from nfs/sunrpc)
158 */
159static int
160smb_move_iov(struct kvec **data, size_t *num, struct kvec *vec, unsigned amount)
161{
162 struct kvec *iv = *data;
163 int i;
164 int len;
165
166 /*
167 * Eat any sent kvecs
168 */
169 while (iv->iov_len <= amount) {
170 amount -= iv->iov_len;
171 iv++;
172 (*num)--;
173 }
174
175 /*
176 * And chew down the partial one
177 */
178 vec[0].iov_len = iv->iov_len-amount;
179 vec[0].iov_base =((unsigned char *)iv->iov_base)+amount;
180 iv++;
181
182 len = vec[0].iov_len;
183
184 /*
185 * And copy any others
186 */
187 for (i = 1; i < *num; i++) {
188 vec[i] = *iv++;
189 len += vec[i].iov_len;
190 }
191
192 *data = vec;
193 return len;
194}
195
196/*
197 * smb_receive_header
198 * Only called by the smbiod thread.
199 */
200int
201smb_receive_header(struct smb_sb_info *server)
202{
203 struct socket *sock;
204 int result = 0;
205 unsigned char peek_buf[4];
206
207 result = -EIO;
208 sock = server_sock(server);
209 if (!sock)
210 goto out;
211 if (sock->sk->sk_state != TCP_ESTABLISHED)
212 goto out;
213
214 if (!server->smb_read) {
215 result = smb_get_length(sock, peek_buf);
216 if (result < 0) {
217 if (result == -ENODATA)
218 result = 0;
219 goto out;
220 }
221 server->smb_len = result + 4;
222
223 if (server->smb_len < SMB_HEADER_LEN) {
224 PARANOIA("short packet: %d\n", result);
225 server->rstate = SMB_RECV_DROP;
226 result = -EIO;
227 goto out;
228 }
229 if (server->smb_len > SMB_MAX_PACKET_SIZE) {
230 PARANOIA("long packet: %d\n", result);
231 server->rstate = SMB_RECV_DROP;
232 result = -EIO;
233 goto out;
234 }
235 }
236
237 result = _recvfrom(sock, server->header + server->smb_read,
238 SMB_HEADER_LEN - server->smb_read, 0);
239 VERBOSE("_recvfrom: %d\n", result);
240 if (result < 0) {
241 VERBOSE("receive error: %d\n", result);
242 goto out;
243 }
244 server->smb_read += result;
245
246 if (server->smb_read == SMB_HEADER_LEN)
247 server->rstate = SMB_RECV_HCOMPLETE;
248out:
249 return result;
250}
251
252static char drop_buffer[PAGE_SIZE];
253
254/*
255 * smb_receive_drop - read and throw away the data
256 * Only called by the smbiod thread.
257 *
258 * FIXME: we are in the kernel, could we just tell the socket that we want
259 * to drop stuff from the buffer?
260 */
261int
262smb_receive_drop(struct smb_sb_info *server)
263{
264 struct socket *sock;
265 unsigned int flags;
266 struct kvec iov;
267 struct msghdr msg;
268 int rlen = smb_len(server->header) - server->smb_read + 4;
269 int result = -EIO;
270
271 if (rlen > PAGE_SIZE)
272 rlen = PAGE_SIZE;
273
274 sock = server_sock(server);
275 if (!sock)
276 goto out;
277 if (sock->sk->sk_state != TCP_ESTABLISHED)
278 goto out;
279
280 flags = MSG_DONTWAIT | MSG_NOSIGNAL;
281 iov.iov_base = drop_buffer;
282 iov.iov_len = PAGE_SIZE;
283 msg.msg_flags = flags;
284 msg.msg_name = NULL;
285 msg.msg_namelen = 0;
286 msg.msg_control = NULL;
287
288 result = kernel_recvmsg(sock, &msg, &iov, 1, rlen, flags);
289
290 VERBOSE("read: %d\n", result);
291 if (result < 0) {
292 VERBOSE("receive error: %d\n", result);
293 goto out;
294 }
295 server->smb_read += result;
296
297 if (server->smb_read >= server->smb_len)
298 server->rstate = SMB_RECV_END;
299
300out:
301 return result;
302}
303
304/*
305 * smb_receive
306 * Only called by the smbiod thread.
307 */
308int
309smb_receive(struct smb_sb_info *server, struct smb_request *req)
310{
311 struct socket *sock;
312 unsigned int flags;
313 struct kvec iov[4];
314 struct kvec *p = req->rq_iov;
315 size_t num = req->rq_iovlen;
316 struct msghdr msg;
317 int rlen;
318 int result = -EIO;
319
320 sock = server_sock(server);
321 if (!sock)
322 goto out;
323 if (sock->sk->sk_state != TCP_ESTABLISHED)
324 goto out;
325
326 flags = MSG_DONTWAIT | MSG_NOSIGNAL;
327 msg.msg_flags = flags;
328 msg.msg_name = NULL;
329 msg.msg_namelen = 0;
330 msg.msg_control = NULL;
331
332 /* Dont repeat bytes and count available bufferspace */
333 rlen = smb_move_iov(&p, &num, iov, req->rq_bytes_recvd);
334 if (req->rq_rlen < rlen)
335 rlen = req->rq_rlen;
336
337 result = kernel_recvmsg(sock, &msg, p, num, rlen, flags);
338
339 VERBOSE("read: %d\n", result);
340 if (result < 0) {
341 VERBOSE("receive error: %d\n", result);
342 goto out;
343 }
344 req->rq_bytes_recvd += result;
345 server->smb_read += result;
346
347out:
348 return result;
349}
350
351/*
352 * Try to send a SMB request. This may return after sending only parts of the
353 * request. SMB_REQ_TRANSMITTED will be set if a request was fully sent.
354 *
355 * Parts of this was taken from xprt_sendmsg from net/sunrpc/xprt.c
356 */
357int
358smb_send_request(struct smb_request *req)
359{
360 struct smb_sb_info *server = req->rq_server;
361 struct socket *sock;
362 struct msghdr msg = {.msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT};
363 int slen = req->rq_slen - req->rq_bytes_sent;
364 int result = -EIO;
365 struct kvec iov[4];
366 struct kvec *p = req->rq_iov;
367 size_t num = req->rq_iovlen;
368
369 sock = server_sock(server);
370 if (!sock)
371 goto out;
372 if (sock->sk->sk_state != TCP_ESTABLISHED)
373 goto out;
374
375 /* Dont repeat bytes */
376 if (req->rq_bytes_sent)
377 smb_move_iov(&p, &num, iov, req->rq_bytes_sent);
378
379 result = kernel_sendmsg(sock, &msg, p, num, slen);
380
381 if (result >= 0) {
382 req->rq_bytes_sent += result;
383 if (req->rq_bytes_sent >= req->rq_slen)
384 req->rq_flags |= SMB_REQ_TRANSMITTED;
385 }
386out:
387 return result;
388}
diff --git a/fs/smbfs/symlink.c b/fs/smbfs/symlink.c
new file mode 100644
index 000000000000..8b069e06433d
--- /dev/null
+++ b/fs/smbfs/symlink.c
@@ -0,0 +1,70 @@
1/*
2 * symlink.c
3 *
4 * Copyright (C) 2002 by John Newbigin
5 *
6 * Please add a note about your changes to smbfs in the ChangeLog file.
7 */
8
9#include <linux/sched.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/fcntl.h>
13#include <linux/stat.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/pagemap.h>
17#include <linux/smp_lock.h>
18#include <linux/net.h>
19#include <linux/namei.h>
20
21#include <asm/uaccess.h>
22#include <asm/system.h>
23
24#include <linux/smbno.h>
25#include <linux/smb_fs.h>
26
27#include "smb_debug.h"
28#include "proto.h"
29
30int smb_symlink(struct inode *inode, struct dentry *dentry, const char *oldname)
31{
32 DEBUG1("create symlink %s -> %s/%s\n", oldname, DENTRY_PATH(dentry));
33
34 return smb_proc_symlink(server_from_dentry(dentry), dentry, oldname);
35}
36
37static int smb_follow_link(struct dentry *dentry, struct nameidata *nd)
38{
39 char *link = __getname();
40 DEBUG1("followlink of %s/%s\n", DENTRY_PATH(dentry));
41
42 if (!link) {
43 link = ERR_PTR(-ENOMEM);
44 } else {
45 int len = smb_proc_read_link(server_from_dentry(dentry),
46 dentry, link, PATH_MAX - 1);
47 if (len < 0) {
48 putname(link);
49 link = ERR_PTR(len);
50 } else {
51 link[len] = 0;
52 }
53 }
54 nd_set_link(nd, link);
55 return 0;
56}
57
58static void smb_put_link(struct dentry *dentry, struct nameidata *nd)
59{
60 char *s = nd_get_link(nd);
61 if (!IS_ERR(s))
62 putname(s);
63}
64
65struct inode_operations smb_link_inode_operations =
66{
67 .readlink = generic_readlink,
68 .follow_link = smb_follow_link,
69 .put_link = smb_put_link,
70};