[ARM] 3607/1: AT91RM9200 misc fixes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / smbfs / dir.c
blob70d9c5a37f5a3c6f7d5cad12e7bad8e1d2e21136
1 /*
2 * dir.c
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
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>
17 #include <linux/smb_fs.h>
18 #include <linux/smb_mount.h>
19 #include <linux/smbno.h>
21 #include "smb_debug.h"
22 #include "proto.h"
24 static int smb_readdir(struct file *, void *, filldir_t);
25 static int smb_dir_open(struct inode *, struct file *);
27 static struct dentry *smb_lookup(struct inode *, struct dentry *, struct nameidata *);
28 static int smb_create(struct inode *, struct dentry *, int, struct nameidata *);
29 static int smb_mkdir(struct inode *, struct dentry *, int);
30 static int smb_rmdir(struct inode *, struct dentry *);
31 static int smb_unlink(struct inode *, struct dentry *);
32 static int smb_rename(struct inode *, struct dentry *,
33 struct inode *, struct dentry *);
34 static int smb_make_node(struct inode *,struct dentry *,int,dev_t);
35 static int smb_link(struct dentry *, struct inode *, struct dentry *);
37 const struct file_operations smb_dir_operations =
39 .read = generic_read_dir,
40 .readdir = smb_readdir,
41 .ioctl = smb_ioctl,
42 .open = smb_dir_open,
45 struct inode_operations smb_dir_inode_operations =
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,
57 struct inode_operations smb_dir_inode_operations_unix =
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,
73 * Read a directory, using filldir to fill the dirent memory.
74 * smb_proc_readdir does the actual reading from the smb server.
76 * The cache code is almost directly taken from ncpfs
78 static int
79 smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
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;
89 ctl.page = NULL;
90 ctl.cache = NULL;
92 VERBOSE("reading %s/%s, f_pos=%d\n",
93 DENTRY_PATH(dentry), (int) filp->f_pos);
95 result = 0;
97 lock_kernel();
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;
112 * Make sure our inode is up-to-date.
114 result = smb_revalidate_inode(dentry);
115 if (result)
116 goto out;
119 page = grab_cache_page(&dir->i_data, 0);
120 if (!page)
121 goto read_really;
123 ctl.cache = cache = kmap(page);
124 ctl.head = cache->head;
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;
132 if (filp->f_pos == 2) {
133 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
134 goto init_cache;
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)
142 * Instead smbfs invalidates its own cache on local changes
143 * and remote changes are not seen until timeout.
147 if (filp->f_pos > ctl.head.end)
148 goto finished;
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;
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;
163 while (ctl.idx < SMB_DIRCACHE_SIZE) {
164 struct dentry *dent;
165 int res;
167 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
168 dentry, filp->f_pos);
169 if (!dent)
170 goto invalid_cache;
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;
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;
190 ctl.idx = 0;
191 ctl.ofs += 1;
193 invalid_cache:
194 if (ctl.page) {
195 kunmap(ctl.page);
196 unlock_page(ctl.page);
197 page_cache_release(ctl.page);
198 ctl.page = NULL;
200 ctl.cache = cache;
201 init_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;
210 read_really:
211 result = server->ops->readdir(filp, dirent, filldir, &ctl);
212 if (result == -ERESTARTSYS && page)
213 ClearPageUptodate(page);
214 if (ctl.idx == -1)
215 goto invalid_cache; /* retry */
216 ctl.head.end = ctl.fpos - 1;
217 ctl.head.eof = ctl.valid;
218 finished:
219 if (page) {
220 cache->head = ctl.head;
221 kunmap(page);
222 if (result != -ERESTARTSYS)
223 SetPageUptodate(page);
224 unlock_page(page);
225 page_cache_release(page);
227 if (ctl.page) {
228 kunmap(ctl.page);
229 SetPageUptodate(ctl.page);
230 unlock_page(ctl.page);
231 page_cache_release(ctl.page);
233 out:
234 unlock_kernel();
235 return result;
238 static int
239 smb_dir_open(struct inode *dir, struct file *file)
241 struct dentry *dentry = file->f_dentry;
242 struct smb_sb_info *server;
243 int error = 0;
245 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
246 file->f_dentry->d_name.name);
249 * Directory timestamps in the core protocol aren't updated
250 * when a file is added, so we give them a very short TTL.
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);
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.
265 if (server->state == CONN_VALID || !IS_ROOT(dentry))
266 error = smb_revalidate_inode(dentry);
267 unlock_kernel();
268 return error;
272 * Dentry operations routines
274 static int smb_lookup_validate(struct dentry *, struct nameidata *);
275 static int smb_hash_dentry(struct dentry *, struct qstr *);
276 static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
277 static int smb_delete_dentry(struct dentry *);
279 static struct dentry_operations smbfs_dentry_operations =
281 .d_revalidate = smb_lookup_validate,
282 .d_hash = smb_hash_dentry,
283 .d_compare = smb_compare_dentry,
284 .d_delete = smb_delete_dentry,
287 static struct dentry_operations smbfs_dentry_operations_case =
289 .d_revalidate = smb_lookup_validate,
290 .d_delete = smb_delete_dentry,
295 * This is the callback when the dcache has a lookup hit.
297 static int
298 smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
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;
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.)
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
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 {
327 * What should we do for negative dentries?
330 return valid;
333 static int
334 smb_hash_dentry(struct dentry *dir, struct qstr *this)
336 unsigned long hash;
337 int i;
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);
344 return 0;
347 static int
348 smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
350 int i, result = 1;
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;
358 result = 0;
359 out:
360 return result;
364 * This is the callback from dput() when d_count is going to 0.
365 * We use this to unhash dentries with bad inodes.
367 static int
368 smb_delete_dentry(struct dentry * dentry)
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;
376 } else {
377 /* N.B. Unhash negative dentries? */
379 return 0;
383 * Initialize a new dentry
385 void
386 smb_new_dentry(struct dentry *dentry)
388 struct smb_sb_info *server = server_from_dentry(dentry);
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;
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?
403 void
404 smb_renew_times(struct dentry * dentry)
406 dget(dentry);
407 spin_lock(&dentry->d_lock);
408 for (;;) {
409 struct dentry *parent;
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);
421 spin_unlock(&dentry->d_lock);
422 dput(dentry);
425 static struct dentry *
426 smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
428 struct smb_fattr finfo;
429 struct inode *inode;
430 int error;
431 struct smb_sb_info *server;
433 error = -ENAMETOOLONG;
434 if (dentry->d_name.len > SMB_MAXNAMELEN)
435 goto out;
437 /* Do not allow lookup of names with backslashes in */
438 error = -EINVAL;
439 if (memchr(dentry->d_name.name, '\\', dentry->d_name.len))
440 goto out;
442 lock_kernel();
443 error = smb_proc_getattr(dentry, &finfo);
444 #ifdef SMBFS_PARANOIA
445 if (error && error != -ENOENT)
446 PARANOIA("find %s/%s failed, error=%d\n",
447 DENTRY_PATH(dentry), error);
448 #endif
450 inode = NULL;
451 if (error == -ENOENT)
452 goto add_entry;
453 if (!error) {
454 error = -EACCES;
455 finfo.f_ino = iunique(dentry->d_sb, 2);
456 inode = smb_iget(dir->i_sb, &finfo);
457 if (inode) {
458 add_entry:
459 server = server_from_dentry(dentry);
460 if (server->mnt->flags & SMB_MOUNT_CASE)
461 dentry->d_op = &smbfs_dentry_operations_case;
462 else
463 dentry->d_op = &smbfs_dentry_operations;
465 d_add(dentry, inode);
466 smb_renew_times(dentry);
467 error = 0;
470 unlock_kernel();
471 out:
472 return ERR_PTR(error);
476 * This code is common to all routines creating a new inode.
478 static int
479 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
481 struct smb_sb_info *server = server_from_dentry(dentry);
482 struct inode *inode;
483 int error;
484 struct smb_fattr fattr;
486 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
488 error = smb_proc_getattr(dentry, &fattr);
489 if (error)
490 goto out_close;
492 smb_renew_times(dentry);
493 fattr.f_ino = iunique(dentry->d_sb, 2);
494 inode = smb_iget(dentry->d_sb, &fattr);
495 if (!inode)
496 goto out_no_inode;
498 if (have_id) {
499 struct smb_inode_info *ei = SMB_I(inode);
500 ei->fileid = fileid;
501 ei->access = SMB_O_RDWR;
502 ei->open = server->generation;
504 d_instantiate(dentry, inode);
505 out:
506 return error;
508 out_no_inode:
509 error = -EACCES;
510 out_close:
511 if (have_id) {
512 PARANOIA("%s/%s failed, error=%d, closing %u\n",
513 DENTRY_PATH(dentry), error, fileid);
514 smb_close_fileid(dentry, fileid);
516 goto out;
519 /* N.B. How should the mode argument be used? */
520 static int
521 smb_create(struct inode *dir, struct dentry *dentry, int mode,
522 struct nameidata *nd)
524 struct smb_sb_info *server = server_from_dentry(dentry);
525 __u16 fileid;
526 int error;
527 struct iattr attr;
529 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
531 lock_kernel();
532 smb_invalid_dir_cache(dir);
533 error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
534 if (!error) {
535 if (server->opt.capabilities & SMB_CAP_UNIX) {
536 /* Set attributes for new file */
537 attr.ia_valid = ATTR_MODE;
538 attr.ia_mode = mode;
539 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
541 error = smb_instantiate(dentry, fileid, 1);
542 } else {
543 PARANOIA("%s/%s failed, error=%d\n",
544 DENTRY_PATH(dentry), error);
546 unlock_kernel();
547 return error;
550 /* N.B. How should the mode argument be used? */
551 static int
552 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
554 struct smb_sb_info *server = server_from_dentry(dentry);
555 int error;
556 struct iattr attr;
558 lock_kernel();
559 smb_invalid_dir_cache(dir);
560 error = smb_proc_mkdir(dentry);
561 if (!error) {
562 if (server->opt.capabilities & SMB_CAP_UNIX) {
563 /* Set attributes for new directory */
564 attr.ia_valid = ATTR_MODE;
565 attr.ia_mode = mode;
566 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
568 error = smb_instantiate(dentry, 0, 0);
570 unlock_kernel();
571 return error;
574 static int
575 smb_rmdir(struct inode *dir, struct dentry *dentry)
577 struct inode *inode = dentry->d_inode;
578 int error;
581 * Close the directory if it's open.
583 lock_kernel();
584 smb_close(inode);
587 * Check that nobody else is using the directory..
589 error = -EBUSY;
590 if (!d_unhashed(dentry))
591 goto out;
593 smb_invalid_dir_cache(dir);
594 error = smb_proc_rmdir(dentry);
596 out:
597 unlock_kernel();
598 return error;
601 static int
602 smb_unlink(struct inode *dir, struct dentry *dentry)
604 int error;
607 * Close the file if it's open.
609 lock_kernel();
610 smb_close(dentry->d_inode);
612 smb_invalid_dir_cache(dir);
613 error = smb_proc_unlink(dentry);
614 if (!error)
615 smb_renew_times(dentry);
616 unlock_kernel();
617 return error;
620 static int
621 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
622 struct inode *new_dir, struct dentry *new_dentry)
624 int error;
627 * Close any open files, and check whether to delete the
628 * target before attempting the rename.
630 lock_kernel();
631 if (old_dentry->d_inode)
632 smb_close(old_dentry->d_inode);
633 if (new_dentry->d_inode) {
634 smb_close(new_dentry->d_inode);
635 error = smb_proc_unlink(new_dentry);
636 if (error) {
637 VERBOSE("unlink %s/%s, error=%d\n",
638 DENTRY_PATH(new_dentry), error);
639 goto out;
641 /* FIXME */
642 d_delete(new_dentry);
645 smb_invalid_dir_cache(old_dir);
646 smb_invalid_dir_cache(new_dir);
647 error = smb_proc_mv(old_dentry, new_dentry);
648 if (!error) {
649 smb_renew_times(old_dentry);
650 smb_renew_times(new_dentry);
652 out:
653 unlock_kernel();
654 return error;
658 * FIXME: samba servers won't let you create device nodes unless uid/gid
659 * matches the connection credentials (and we don't know which those are ...)
661 static int
662 smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
664 int error;
665 struct iattr attr;
667 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
668 attr.ia_mode = mode;
669 attr.ia_uid = current->euid;
670 attr.ia_gid = current->egid;
672 if (!new_valid_dev(dev))
673 return -EINVAL;
675 smb_invalid_dir_cache(dir);
676 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
677 if (!error) {
678 error = smb_instantiate(dentry, 0, 0);
680 return error;
684 * dentry = existing file
685 * new_dentry = new file
687 static int
688 smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
690 int error;
692 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
693 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
694 smb_invalid_dir_cache(dir);
695 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
696 if (!error) {
697 smb_renew_times(dentry);
698 error = smb_instantiate(new_dentry, 0, 0);
700 return error;