blob: 34c7a11d91f0ebbb29854a0116d8c558825fbbbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080037const struct file_operations smb_dir_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
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);
Andrew Mortoncaf73602006-02-01 03:04:39 -0800212 if (result == -ERESTARTSYS && page)
213 ClearPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (ctl.idx == -1)
215 goto invalid_cache; /* retry */
216 ctl.head.end = ctl.fpos - 1;
217 ctl.head.eof = ctl.valid;
218finished:
219 if (page) {
220 cache->head = ctl.head;
221 kunmap(page);
Andrew Mortoncaf73602006-02-01 03:04:39 -0800222 if (result != -ERESTARTSYS)
223 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unlock_page(page);
225 page_cache_release(page);
226 }
227 if (ctl.page) {
228 kunmap(ctl.page);
229 SetPageUptodate(ctl.page);
230 unlock_page(ctl.page);
231 page_cache_release(ctl.page);
232 }
233out:
234 unlock_kernel();
235 return result;
236}
237
238static int
239smb_dir_open(struct inode *dir, struct file *file)
240{
241 struct dentry *dentry = file->f_dentry;
242 struct smb_sb_info *server;
243 int error = 0;
244
245 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
246 file->f_dentry->d_name.name);
247
248 /*
249 * Directory timestamps in the core protocol aren't updated
250 * when a file is added, so we give them a very short TTL.
251 */
252 lock_kernel();
253 server = server_from_dentry(dentry);
254 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
255 unsigned long age = jiffies - SMB_I(dir)->oldmtime;
256 if (age > 2*HZ)
257 smb_invalid_dir_cache(dir);
258 }
259
260 /*
261 * Note: in order to allow the smbmount process to open the
262 * mount point, we only revalidate if the connection is valid or
263 * if the process is trying to access something other than the root.
264 */
265 if (server->state == CONN_VALID || !IS_ROOT(dentry))
266 error = smb_revalidate_inode(dentry);
267 unlock_kernel();
268 return error;
269}
270
271/*
272 * Dentry operations routines
273 */
274static int smb_lookup_validate(struct dentry *, struct nameidata *);
275static int smb_hash_dentry(struct dentry *, struct qstr *);
276static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
277static int smb_delete_dentry(struct dentry *);
278
279static struct dentry_operations smbfs_dentry_operations =
280{
281 .d_revalidate = smb_lookup_validate,
282 .d_hash = smb_hash_dentry,
283 .d_compare = smb_compare_dentry,
284 .d_delete = smb_delete_dentry,
285};
286
287static struct dentry_operations smbfs_dentry_operations_case =
288{
289 .d_revalidate = smb_lookup_validate,
290 .d_delete = smb_delete_dentry,
291};
292
293
294/*
295 * This is the callback when the dcache has a lookup hit.
296 */
297static int
298smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
299{
300 struct smb_sb_info *server = server_from_dentry(dentry);
301 struct inode * inode = dentry->d_inode;
302 unsigned long age = jiffies - dentry->d_time;
303 int valid;
304
305 /*
306 * The default validation is based on dentry age:
307 * we believe in dentries for a few seconds. (But each
308 * successful server lookup renews the timestamp.)
309 */
310 valid = (age <= SMB_MAX_AGE(server));
311#ifdef SMBFS_DEBUG_VERBOSE
312 if (!valid)
313 VERBOSE("%s/%s not valid, age=%lu\n",
314 DENTRY_PATH(dentry), age);
315#endif
316
317 if (inode) {
318 lock_kernel();
319 if (is_bad_inode(inode)) {
320 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
321 valid = 0;
322 } else if (!valid)
323 valid = (smb_revalidate_inode(dentry) == 0);
324 unlock_kernel();
325 } else {
326 /*
327 * What should we do for negative dentries?
328 */
329 }
330 return valid;
331}
332
333static int
334smb_hash_dentry(struct dentry *dir, struct qstr *this)
335{
336 unsigned long hash;
337 int i;
338
339 hash = init_name_hash();
340 for (i=0; i < this->len ; i++)
341 hash = partial_name_hash(tolower(this->name[i]), hash);
342 this->hash = end_name_hash(hash);
343
344 return 0;
345}
346
347static int
348smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
349{
350 int i, result = 1;
351
352 if (a->len != b->len)
353 goto out;
354 for (i=0; i < a->len; i++) {
355 if (tolower(a->name[i]) != tolower(b->name[i]))
356 goto out;
357 }
358 result = 0;
359out:
360 return result;
361}
362
363/*
364 * This is the callback from dput() when d_count is going to 0.
365 * We use this to unhash dentries with bad inodes.
366 */
367static int
368smb_delete_dentry(struct dentry * dentry)
369{
370 if (dentry->d_inode) {
371 if (is_bad_inode(dentry->d_inode)) {
372 PARANOIA("bad inode, unhashing %s/%s\n",
373 DENTRY_PATH(dentry));
374 return 1;
375 }
376 } else {
377 /* N.B. Unhash negative dentries? */
378 }
379 return 0;
380}
381
382/*
383 * Initialize a new dentry
384 */
385void
386smb_new_dentry(struct dentry *dentry)
387{
388 struct smb_sb_info *server = server_from_dentry(dentry);
389
390 if (server->mnt->flags & SMB_MOUNT_CASE)
391 dentry->d_op = &smbfs_dentry_operations_case;
392 else
393 dentry->d_op = &smbfs_dentry_operations;
394 dentry->d_time = jiffies;
395}
396
397
398/*
399 * Whenever a lookup succeeds, we know the parent directories
400 * are all valid, so we want to update the dentry timestamps.
401 * N.B. Move this to dcache?
402 */
403void
404smb_renew_times(struct dentry * dentry)
405{
406 dget(dentry);
407 spin_lock(&dentry->d_lock);
408 for (;;) {
409 struct dentry *parent;
410
411 dentry->d_time = jiffies;
412 if (IS_ROOT(dentry))
413 break;
414 parent = dentry->d_parent;
415 dget(parent);
416 spin_unlock(&dentry->d_lock);
417 dput(dentry);
418 dentry = parent;
419 spin_lock(&dentry->d_lock);
420 }
421 spin_unlock(&dentry->d_lock);
422 dput(dentry);
423}
424
425static struct dentry *
426smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
427{
428 struct smb_fattr finfo;
429 struct inode *inode;
430 int error;
431 struct smb_sb_info *server;
432
433 error = -ENAMETOOLONG;
434 if (dentry->d_name.len > SMB_MAXNAMELEN)
435 goto out;
436
437 lock_kernel();
438 error = smb_proc_getattr(dentry, &finfo);
439#ifdef SMBFS_PARANOIA
440 if (error && error != -ENOENT)
441 PARANOIA("find %s/%s failed, error=%d\n",
442 DENTRY_PATH(dentry), error);
443#endif
444
445 inode = NULL;
446 if (error == -ENOENT)
447 goto add_entry;
448 if (!error) {
449 error = -EACCES;
450 finfo.f_ino = iunique(dentry->d_sb, 2);
451 inode = smb_iget(dir->i_sb, &finfo);
452 if (inode) {
453 add_entry:
454 server = server_from_dentry(dentry);
455 if (server->mnt->flags & SMB_MOUNT_CASE)
456 dentry->d_op = &smbfs_dentry_operations_case;
457 else
458 dentry->d_op = &smbfs_dentry_operations;
459
460 d_add(dentry, inode);
461 smb_renew_times(dentry);
462 error = 0;
463 }
464 }
465 unlock_kernel();
466out:
467 return ERR_PTR(error);
468}
469
470/*
471 * This code is common to all routines creating a new inode.
472 */
473static int
474smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
475{
476 struct smb_sb_info *server = server_from_dentry(dentry);
477 struct inode *inode;
478 int error;
479 struct smb_fattr fattr;
480
481 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
482
483 error = smb_proc_getattr(dentry, &fattr);
484 if (error)
485 goto out_close;
486
487 smb_renew_times(dentry);
488 fattr.f_ino = iunique(dentry->d_sb, 2);
489 inode = smb_iget(dentry->d_sb, &fattr);
490 if (!inode)
491 goto out_no_inode;
492
493 if (have_id) {
494 struct smb_inode_info *ei = SMB_I(inode);
495 ei->fileid = fileid;
496 ei->access = SMB_O_RDWR;
497 ei->open = server->generation;
498 }
499 d_instantiate(dentry, inode);
500out:
501 return error;
502
503out_no_inode:
504 error = -EACCES;
505out_close:
506 if (have_id) {
507 PARANOIA("%s/%s failed, error=%d, closing %u\n",
508 DENTRY_PATH(dentry), error, fileid);
509 smb_close_fileid(dentry, fileid);
510 }
511 goto out;
512}
513
514/* N.B. How should the mode argument be used? */
515static int
516smb_create(struct inode *dir, struct dentry *dentry, int mode,
517 struct nameidata *nd)
518{
519 struct smb_sb_info *server = server_from_dentry(dentry);
520 __u16 fileid;
521 int error;
522 struct iattr attr;
523
524 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
525
526 lock_kernel();
527 smb_invalid_dir_cache(dir);
528 error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
529 if (!error) {
530 if (server->opt.capabilities & SMB_CAP_UNIX) {
531 /* Set attributes for new file */
532 attr.ia_valid = ATTR_MODE;
533 attr.ia_mode = mode;
534 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
535 }
536 error = smb_instantiate(dentry, fileid, 1);
537 } else {
538 PARANOIA("%s/%s failed, error=%d\n",
539 DENTRY_PATH(dentry), error);
540 }
541 unlock_kernel();
542 return error;
543}
544
545/* N.B. How should the mode argument be used? */
546static int
547smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
548{
549 struct smb_sb_info *server = server_from_dentry(dentry);
550 int error;
551 struct iattr attr;
552
553 lock_kernel();
554 smb_invalid_dir_cache(dir);
555 error = smb_proc_mkdir(dentry);
556 if (!error) {
557 if (server->opt.capabilities & SMB_CAP_UNIX) {
558 /* Set attributes for new directory */
559 attr.ia_valid = ATTR_MODE;
560 attr.ia_mode = mode;
561 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
562 }
563 error = smb_instantiate(dentry, 0, 0);
564 }
565 unlock_kernel();
566 return error;
567}
568
569static int
570smb_rmdir(struct inode *dir, struct dentry *dentry)
571{
572 struct inode *inode = dentry->d_inode;
573 int error;
574
575 /*
576 * Close the directory if it's open.
577 */
578 lock_kernel();
579 smb_close(inode);
580
581 /*
582 * Check that nobody else is using the directory..
583 */
584 error = -EBUSY;
585 if (!d_unhashed(dentry))
586 goto out;
587
588 smb_invalid_dir_cache(dir);
589 error = smb_proc_rmdir(dentry);
590
591out:
592 unlock_kernel();
593 return error;
594}
595
596static int
597smb_unlink(struct inode *dir, struct dentry *dentry)
598{
599 int error;
600
601 /*
602 * Close the file if it's open.
603 */
604 lock_kernel();
605 smb_close(dentry->d_inode);
606
607 smb_invalid_dir_cache(dir);
608 error = smb_proc_unlink(dentry);
609 if (!error)
610 smb_renew_times(dentry);
611 unlock_kernel();
612 return error;
613}
614
615static int
616smb_rename(struct inode *old_dir, struct dentry *old_dentry,
617 struct inode *new_dir, struct dentry *new_dentry)
618{
619 int error;
620
621 /*
622 * Close any open files, and check whether to delete the
623 * target before attempting the rename.
624 */
625 lock_kernel();
626 if (old_dentry->d_inode)
627 smb_close(old_dentry->d_inode);
628 if (new_dentry->d_inode) {
629 smb_close(new_dentry->d_inode);
630 error = smb_proc_unlink(new_dentry);
631 if (error) {
632 VERBOSE("unlink %s/%s, error=%d\n",
633 DENTRY_PATH(new_dentry), error);
634 goto out;
635 }
636 /* FIXME */
637 d_delete(new_dentry);
638 }
639
640 smb_invalid_dir_cache(old_dir);
641 smb_invalid_dir_cache(new_dir);
642 error = smb_proc_mv(old_dentry, new_dentry);
643 if (!error) {
644 smb_renew_times(old_dentry);
645 smb_renew_times(new_dentry);
646 }
647out:
648 unlock_kernel();
649 return error;
650}
651
652/*
653 * FIXME: samba servers won't let you create device nodes unless uid/gid
654 * matches the connection credentials (and we don't know which those are ...)
655 */
656static int
657smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
658{
659 int error;
660 struct iattr attr;
661
662 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
663 attr.ia_mode = mode;
664 attr.ia_uid = current->euid;
665 attr.ia_gid = current->egid;
666
667 if (!new_valid_dev(dev))
668 return -EINVAL;
669
670 smb_invalid_dir_cache(dir);
671 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
672 if (!error) {
673 error = smb_instantiate(dentry, 0, 0);
674 }
675 return error;
676}
677
678/*
679 * dentry = existing file
680 * new_dentry = new file
681 */
682static int
683smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
684{
685 int error;
686
687 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
688 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
689 smb_invalid_dir_cache(dir);
690 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
691 if (!error) {
692 smb_renew_times(dentry);
693 error = smb_instantiate(new_dentry, 0, 0);
694 }
695 return error;
696}