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.
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/smp_lock.h>
14 #include <linux/ctype.h>
16 #include <linux/smb_fs.h>
17 #include <linux/smb_mount.h>
18 #include <linux/smbno.h>
20 #include "smb_debug.h"
23 static int smb_readdir(struct file *, void *, filldir_t);
24 static int smb_dir_open(struct inode *, struct file *);
26 static struct dentry *smb_lookup(struct inode *, struct dentry *);
27 static int smb_create(struct inode *, struct dentry *, int);
28 static int smb_mkdir(struct inode *, struct dentry *, int);
29 static int smb_rmdir(struct inode *, struct dentry *);
30 static int smb_unlink(struct inode *, struct dentry *);
31 static int smb_rename(struct inode *, struct dentry *,
32 struct inode *, struct dentry *);
33 static int smb_make_node(struct inode *,struct dentry *, int, int);
34 static int smb_link(struct dentry *, struct inode *, struct dentry *);
36 struct file_operations smb_dir_operations =
38 read: generic_read_dir,
44 struct inode_operations smb_dir_inode_operations =
52 revalidate: smb_revalidate_inode,
53 setattr: smb_notify_change,
56 struct inode_operations smb_dir_inode_operations_unix =
64 revalidate: smb_revalidate_inode,
65 setattr: smb_notify_change,
72 * Read a directory, using filldir to fill the dirent memory.
73 * smb_proc_readdir does the actual reading from the smb server.
75 * The cache code is almost directly taken from ncpfs
78 smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
80 struct dentry *dentry = filp->f_dentry;
81 struct inode *dir = dentry->d_inode;
82 struct smb_sb_info *server = server_from_dentry(dentry);
83 union smb_dir_cache *cache = NULL;
84 struct smb_cache_control ctl;
85 struct page *page = NULL;
91 VERBOSE("reading %s/%s, f_pos=%d\n",
92 DENTRY_PATH(dentry), (int) filp->f_pos);
95 switch ((unsigned int) filp->f_pos) {
97 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
102 if (filldir(dirent, "..", 2, 1,
103 dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
109 * Make sure our inode is up-to-date.
111 result = smb_revalidate_inode(dentry);
116 page = grab_cache_page(&dir->i_data, 0);
120 ctl.cache = cache = kmap(page);
121 ctl.head = cache->head;
123 if (!Page_Uptodate(page) || !ctl.head.eof) {
124 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
125 DENTRY_PATH(dentry), Page_Uptodate(page),ctl.head.eof);
129 if (filp->f_pos == 2) {
130 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
134 * N.B. ncpfs checks mtime of dentry too here, we don't.
135 * 1. common smb servers do not update mtime on dir changes
136 * 2. it requires an extra smb request
137 * (revalidate has the same timeout as ctl.head.time)
139 * Instead smbfs invalidates its own cache on local changes
140 * and remote changes are not seen until timeout.
144 if (filp->f_pos > ctl.head.end)
147 ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
148 ctl.ofs = ctl.fpos / SMB_DIRCACHE_SIZE;
149 ctl.idx = ctl.fpos % SMB_DIRCACHE_SIZE;
153 ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
156 ctl.cache = kmap(ctl.page);
157 if (!Page_Uptodate(ctl.page))
160 while (ctl.idx < SMB_DIRCACHE_SIZE) {
164 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
165 dentry, filp->f_pos);
169 res = filldir(dirent, dent->d_name.name,
170 dent->d_name.len, filp->f_pos,
171 dent->d_inode->i_ino, DT_UNKNOWN);
177 if (filp->f_pos > ctl.head.end)
182 SetPageUptodate(ctl.page);
183 UnlockPage(ctl.page);
184 page_cache_release(ctl.page);
193 UnlockPage(ctl.page);
194 page_cache_release(ctl.page);
199 smb_invalidate_dircache_entries(dentry);
200 ctl.head.time = jiffies;
204 ctl.idx = SMB_DIRCACHE_START;
208 result = server->ops->readdir(filp, dirent, filldir, &ctl);
210 goto invalid_cache; /* retry */
211 ctl.head.end = ctl.fpos - 1;
212 ctl.head.eof = ctl.valid;
215 cache->head = ctl.head;
217 SetPageUptodate(page);
219 page_cache_release(page);
223 SetPageUptodate(ctl.page);
224 UnlockPage(ctl.page);
225 page_cache_release(ctl.page);
232 smb_dir_open(struct inode *dir, struct file *file)
234 struct dentry *dentry = file->f_dentry;
235 struct smb_sb_info *server;
238 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
239 file->f_dentry->d_name.name);
242 * Directory timestamps in the core protocol aren't updated
243 * when a file is added, so we give them a very short TTL.
246 server = server_from_dentry(dentry);
247 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
248 unsigned long age = jiffies - dir->u.smbfs_i.oldmtime;
250 smb_invalid_dir_cache(dir);
254 * Note: in order to allow the smbmount process to open the
255 * mount point, we only revalidate if the connection is valid or
256 * if the process is trying to access something other than the root.
258 if (server->state == CONN_VALID || !IS_ROOT(dentry))
259 error = smb_revalidate_inode(dentry);
265 * Dentry operations routines
267 static int smb_lookup_validate(struct dentry *, int);
268 static int smb_hash_dentry(struct dentry *, struct qstr *);
269 static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
270 static int smb_delete_dentry(struct dentry *);
272 static struct dentry_operations smbfs_dentry_operations =
274 d_revalidate: smb_lookup_validate,
275 d_hash: smb_hash_dentry,
276 d_compare: smb_compare_dentry,
277 d_delete: smb_delete_dentry,
280 static struct dentry_operations smbfs_dentry_operations_case =
282 d_revalidate: smb_lookup_validate,
283 d_delete: smb_delete_dentry,
288 * This is the callback when the dcache has a lookup hit.
291 smb_lookup_validate(struct dentry * dentry, int flags)
293 struct smb_sb_info *server = server_from_dentry(dentry);
294 struct inode * inode = dentry->d_inode;
295 unsigned long age = jiffies - dentry->d_time;
299 * The default validation is based on dentry age:
300 * we believe in dentries for a few seconds. (But each
301 * successful server lookup renews the timestamp.)
303 valid = (age <= SMB_MAX_AGE(server));
304 #ifdef SMBFS_DEBUG_VERBOSE
306 VERBOSE("%s/%s not valid, age=%lu\n",
307 DENTRY_PATH(dentry), age);
312 if (is_bad_inode(inode)) {
313 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
316 valid = (smb_revalidate_inode(dentry) == 0);
320 * What should we do for negative dentries?
327 smb_hash_dentry(struct dentry *dir, struct qstr *this)
332 hash = init_name_hash();
333 for (i=0; i < this->len ; i++)
334 hash = partial_name_hash(tolower(this->name[i]), hash);
335 this->hash = end_name_hash(hash);
341 smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
345 if (a->len != b->len)
347 for (i=0; i < a->len; i++) {
348 if (tolower(a->name[i]) != tolower(b->name[i]))
357 * This is the callback from dput() when d_count is going to 0.
358 * We use this to unhash dentries with bad inodes.
361 smb_delete_dentry(struct dentry * dentry)
363 if (dentry->d_inode) {
364 if (is_bad_inode(dentry->d_inode)) {
365 PARANOIA("bad inode, unhashing %s/%s\n",
366 DENTRY_PATH(dentry));
370 /* N.B. Unhash negative dentries? */
376 * Initialize a new dentry
379 smb_new_dentry(struct dentry *dentry)
381 struct smb_sb_info *server = server_from_dentry(dentry);
383 if (server->mnt->flags & SMB_MOUNT_CASE)
384 dentry->d_op = &smbfs_dentry_operations_case;
386 dentry->d_op = &smbfs_dentry_operations;
387 dentry->d_time = jiffies;
392 * Whenever a lookup succeeds, we know the parent directories
393 * are all valid, so we want to update the dentry timestamps.
394 * N.B. Move this to dcache?
397 smb_renew_times(struct dentry * dentry)
400 dentry->d_time = jiffies;
403 dentry = dentry->d_parent;
407 static struct dentry *
408 smb_lookup(struct inode *dir, struct dentry *dentry)
410 struct smb_fattr finfo;
413 struct smb_sb_info *server;
415 error = -ENAMETOOLONG;
416 if (dentry->d_name.len > SMB_MAXNAMELEN)
419 /* Do not allow lookup of names with backslashes in */
421 if (memchr(dentry->d_name.name, '\\', dentry->d_name.len))
424 error = smb_proc_getattr(dentry, &finfo);
425 #ifdef SMBFS_PARANOIA
426 if (error && error != -ENOENT)
427 PARANOIA("find %s/%s failed, error=%d\n",
428 DENTRY_PATH(dentry), error);
432 if (error == -ENOENT)
436 finfo.f_ino = iunique(dentry->d_sb, 2);
437 inode = smb_iget(dir->i_sb, &finfo);
440 server = server_from_dentry(dentry);
441 if (server->mnt->flags & SMB_MOUNT_CASE)
442 dentry->d_op = &smbfs_dentry_operations_case;
444 dentry->d_op = &smbfs_dentry_operations;
446 d_add(dentry, inode);
447 smb_renew_times(dentry);
452 return ERR_PTR(error);
456 * This code is common to all routines creating a new inode.
459 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
461 struct smb_sb_info *server = server_from_dentry(dentry);
464 struct smb_fattr fattr;
466 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
468 error = smb_proc_getattr(dentry, &fattr);
472 smb_renew_times(dentry);
473 fattr.f_ino = iunique(dentry->d_sb, 2);
474 inode = smb_iget(dentry->d_sb, &fattr);
479 inode->u.smbfs_i.fileid = fileid;
480 inode->u.smbfs_i.access = SMB_O_RDWR;
481 inode->u.smbfs_i.open = server->generation;
483 d_instantiate(dentry, inode);
491 PARANOIA("%s/%s failed, error=%d, closing %u\n",
492 DENTRY_PATH(dentry), error, fileid);
493 smb_close_fileid(dentry, fileid);
498 /* N.B. How should the mode argument be used? */
500 smb_create(struct inode *dir, struct dentry *dentry, int mode)
502 struct smb_sb_info *server = server_from_dentry(dentry);
507 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
509 smb_invalid_dir_cache(dir);
510 error = smb_proc_create(dentry, 0, CURRENT_TIME, &fileid);
512 if (server->opt.capabilities & SMB_CAP_UNIX) {
513 /* Set attributes for new file */
514 attr.ia_valid = ATTR_MODE;
516 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
518 error = smb_instantiate(dentry, fileid, 1);
520 PARANOIA("%s/%s failed, error=%d\n",
521 DENTRY_PATH(dentry), error);
526 /* N.B. How should the mode argument be used? */
528 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
530 struct smb_sb_info *server = server_from_dentry(dentry);
534 smb_invalid_dir_cache(dir);
535 error = smb_proc_mkdir(dentry);
537 if (server->opt.capabilities & SMB_CAP_UNIX) {
538 /* Set attributes for new directory */
539 attr.ia_valid = ATTR_MODE;
541 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
543 error = smb_instantiate(dentry, 0, 0);
549 smb_rmdir(struct inode *dir, struct dentry *dentry)
551 struct inode *inode = dentry->d_inode;
555 * Close the directory if it's open.
560 * Check that nobody else is using the directory..
563 if (!d_unhashed(dentry))
566 smb_invalid_dir_cache(dir);
567 error = smb_proc_rmdir(dentry);
574 smb_unlink(struct inode *dir, struct dentry *dentry)
579 * Close the file if it's open.
581 smb_close(dentry->d_inode);
583 smb_invalid_dir_cache(dir);
584 error = smb_proc_unlink(dentry);
586 smb_renew_times(dentry);
591 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
592 struct inode *new_dir, struct dentry *new_dentry)
597 * Close any open files, and check whether to delete the
598 * target before attempting the rename.
600 if (old_dentry->d_inode)
601 smb_close(old_dentry->d_inode);
602 if (new_dentry->d_inode) {
603 smb_close(new_dentry->d_inode);
604 error = smb_proc_unlink(new_dentry);
606 VERBOSE("unlink %s/%s, error=%d\n",
607 DENTRY_PATH(new_dentry), error);
611 d_delete(new_dentry);
614 smb_invalid_dir_cache(old_dir);
615 smb_invalid_dir_cache(new_dir);
616 error = smb_proc_mv(old_dentry, new_dentry);
618 smb_renew_times(old_dentry);
619 smb_renew_times(new_dentry);
626 * FIXME: samba servers won't let you create device nodes unless uid/gid
627 * matches the connection credentials (and we don't know which those are ...)
630 smb_make_node(struct inode *dir, struct dentry *dentry, int mode, int dev)
635 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
637 attr.ia_uid = current->euid;
638 attr.ia_gid = current->egid;
640 smb_invalid_dir_cache(dir);
641 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
643 error = smb_instantiate(dentry, 0, 0);
649 * dentry = existing file
650 * new_dentry = new file
653 smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
657 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
658 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
659 smb_invalid_dir_cache(dir);
660 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
662 smb_renew_times(dentry);
663 error = smb_instantiate(new_dentry, 0, 0);