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/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 #include <linux/sched.h>
19 #include "smb_mount.h"
22 #include "smb_debug.h"
25 static int smb_readdir(struct file *, void *, filldir_t);
26 static int smb_dir_open(struct inode *, struct file *);
28 static struct dentry *smb_lookup(struct inode *, struct dentry *, struct nameidata *);
29 static int smb_create(struct inode *, struct dentry *, int, struct nameidata *);
30 static int smb_mkdir(struct inode *, struct dentry *, int);
31 static int smb_rmdir(struct inode *, struct dentry *);
32 static int smb_unlink(struct inode *, struct dentry *);
33 static int smb_rename(struct inode *, struct dentry *,
34 struct inode *, struct dentry *);
35 static int smb_make_node(struct inode *,struct dentry *,int,dev_t);
36 static int smb_link(struct dentry *, struct inode *, struct dentry *);
38 const struct file_operations smb_dir_operations =
40 .llseek = generic_file_llseek,
41 .read = generic_read_dir,
42 .readdir = smb_readdir,
43 .unlocked_ioctl = smb_ioctl,
47 const struct inode_operations smb_dir_inode_operations =
55 .getattr = smb_getattr,
56 .setattr = smb_notify_change,
59 const struct inode_operations smb_dir_inode_operations_unix =
67 .getattr = smb_getattr,
68 .setattr = smb_notify_change,
69 .symlink = smb_symlink,
70 .mknod = smb_make_node,
75 * Read a directory, using filldir to fill the dirent memory.
76 * smb_proc_readdir does the actual reading from the smb server.
78 * The cache code is almost directly taken from ncpfs
81 smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
83 struct dentry *dentry = filp->f_path.dentry;
84 struct inode *dir = dentry->d_inode;
85 struct smb_sb_info *server = server_from_dentry(dentry);
86 union smb_dir_cache *cache = NULL;
87 struct smb_cache_control ctl;
88 struct page *page = NULL;
94 VERBOSE("reading %s/%s, f_pos=%d\n",
95 DENTRY_PATH(dentry), (int) filp->f_pos);
101 switch ((unsigned int) filp->f_pos) {
103 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
108 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR) < 0)
114 * Make sure our inode is up-to-date.
116 result = smb_revalidate_inode(dentry);
121 page = grab_cache_page(&dir->i_data, 0);
125 ctl.cache = cache = kmap(page);
126 ctl.head = cache->head;
128 if (!PageUptodate(page) || !ctl.head.eof) {
129 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
130 DENTRY_PATH(dentry), PageUptodate(page),ctl.head.eof);
134 if (filp->f_pos == 2) {
135 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
139 * N.B. ncpfs checks mtime of dentry too here, we don't.
140 * 1. common smb servers do not update mtime on dir changes
141 * 2. it requires an extra smb request
142 * (revalidate has the same timeout as ctl.head.time)
144 * Instead smbfs invalidates its own cache on local changes
145 * and remote changes are not seen until timeout.
149 if (filp->f_pos > ctl.head.end)
152 ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
153 ctl.ofs = ctl.fpos / SMB_DIRCACHE_SIZE;
154 ctl.idx = ctl.fpos % SMB_DIRCACHE_SIZE;
158 ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
161 ctl.cache = kmap(ctl.page);
162 if (!PageUptodate(ctl.page))
165 while (ctl.idx < SMB_DIRCACHE_SIZE) {
169 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
170 dentry, filp->f_pos);
174 res = filldir(dirent, dent->d_name.name,
175 dent->d_name.len, filp->f_pos,
176 dent->d_inode->i_ino, DT_UNKNOWN);
182 if (filp->f_pos > ctl.head.end)
187 SetPageUptodate(ctl.page);
188 unlock_page(ctl.page);
189 page_cache_release(ctl.page);
198 unlock_page(ctl.page);
199 page_cache_release(ctl.page);
204 smb_invalidate_dircache_entries(dentry);
205 ctl.head.time = jiffies;
209 ctl.idx = SMB_DIRCACHE_START;
213 result = server->ops->readdir(filp, dirent, filldir, &ctl);
214 if (result == -ERESTARTSYS && page)
215 ClearPageUptodate(page);
217 goto invalid_cache; /* retry */
218 ctl.head.end = ctl.fpos - 1;
219 ctl.head.eof = ctl.valid;
222 cache->head = ctl.head;
224 if (result != -ERESTARTSYS)
225 SetPageUptodate(page);
227 page_cache_release(page);
231 SetPageUptodate(ctl.page);
232 unlock_page(ctl.page);
233 page_cache_release(ctl.page);
241 smb_dir_open(struct inode *dir, struct file *file)
243 struct dentry *dentry = file->f_path.dentry;
244 struct smb_sb_info *server;
247 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
248 file->f_path.dentry->d_name.name);
251 * Directory timestamps in the core protocol aren't updated
252 * when a file is added, so we give them a very short TTL.
255 server = server_from_dentry(dentry);
256 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
257 unsigned long age = jiffies - SMB_I(dir)->oldmtime;
259 smb_invalid_dir_cache(dir);
263 * Note: in order to allow the smbmount process to open the
264 * mount point, we only revalidate if the connection is valid or
265 * if the process is trying to access something other than the root.
267 if (server->state == CONN_VALID || !IS_ROOT(dentry))
268 error = smb_revalidate_inode(dentry);
274 * Dentry operations routines
276 static int smb_lookup_validate(struct dentry *, struct nameidata *);
277 static int smb_hash_dentry(const struct dentry *, const struct inode *,
279 static int smb_compare_dentry(const struct dentry *,
280 const struct inode *,
281 const struct dentry *, const struct inode *,
282 unsigned int, const char *, const struct qstr *);
283 static int smb_delete_dentry(const struct dentry *);
285 static const struct dentry_operations smbfs_dentry_operations =
287 .d_revalidate = smb_lookup_validate,
288 .d_hash = smb_hash_dentry,
289 .d_compare = smb_compare_dentry,
290 .d_delete = smb_delete_dentry,
293 static const struct dentry_operations smbfs_dentry_operations_case =
295 .d_revalidate = smb_lookup_validate,
296 .d_delete = smb_delete_dentry,
301 * This is the callback when the dcache has a lookup hit.
304 smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
306 struct smb_sb_info *server = server_from_dentry(dentry);
307 struct inode * inode = dentry->d_inode;
308 unsigned long age = jiffies - dentry->d_time;
312 * The default validation is based on dentry age:
313 * we believe in dentries for a few seconds. (But each
314 * successful server lookup renews the timestamp.)
316 valid = (age <= SMB_MAX_AGE(server));
317 #ifdef SMBFS_DEBUG_VERBOSE
319 VERBOSE("%s/%s not valid, age=%lu\n",
320 DENTRY_PATH(dentry), age);
325 if (is_bad_inode(inode)) {
326 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
329 valid = (smb_revalidate_inode(dentry) == 0);
333 * What should we do for negative dentries?
340 smb_hash_dentry(const struct dentry *dir, const struct inode *inode,
346 hash = init_name_hash();
347 for (i=0; i < this->len ; i++)
348 hash = partial_name_hash(tolower(this->name[i]), hash);
349 this->hash = end_name_hash(hash);
355 smb_compare_dentry(const struct dentry *parent,
356 const struct inode *pinode,
357 const struct dentry *dentry, const struct inode *inode,
358 unsigned int len, const char *str, const struct qstr *name)
362 if (len != name->len)
364 for (i=0; i < len; i++) {
365 if (tolower(str[i]) != tolower(name->name[i]))
374 * This is the callback from dput() when d_count is going to 0.
375 * We use this to unhash dentries with bad inodes.
378 smb_delete_dentry(const struct dentry *dentry)
380 if (dentry->d_inode) {
381 if (is_bad_inode(dentry->d_inode)) {
382 PARANOIA("bad inode, unhashing %s/%s\n",
383 DENTRY_PATH(dentry));
387 /* N.B. Unhash negative dentries? */
393 * Initialize a new dentry
396 smb_new_dentry(struct dentry *dentry)
398 struct smb_sb_info *server = server_from_dentry(dentry);
400 if (server->mnt->flags & SMB_MOUNT_CASE)
401 d_set_d_op(dentry, &smbfs_dentry_operations_case);
403 d_set_d_op(dentry, &smbfs_dentry_operations);
404 dentry->d_time = jiffies;
409 * Whenever a lookup succeeds, we know the parent directories
410 * are all valid, so we want to update the dentry timestamps.
411 * N.B. Move this to dcache?
414 smb_renew_times(struct dentry * dentry)
417 dentry->d_time = jiffies;
419 while (!IS_ROOT(dentry)) {
420 struct dentry *parent = dget_parent(dentry);
424 dentry->d_time = jiffies;
429 static struct dentry *
430 smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
432 struct smb_fattr finfo;
435 struct smb_sb_info *server;
437 error = -ENAMETOOLONG;
438 if (dentry->d_name.len > SMB_MAXNAMELEN)
441 /* Do not allow lookup of names with backslashes in */
443 if (memchr(dentry->d_name.name, '\\', dentry->d_name.len))
447 error = smb_proc_getattr(dentry, &finfo);
448 #ifdef SMBFS_PARANOIA
449 if (error && error != -ENOENT)
450 PARANOIA("find %s/%s failed, error=%d\n",
451 DENTRY_PATH(dentry), error);
455 if (error == -ENOENT)
459 finfo.f_ino = iunique(dentry->d_sb, 2);
460 inode = smb_iget(dir->i_sb, &finfo);
463 server = server_from_dentry(dentry);
464 if (server->mnt->flags & SMB_MOUNT_CASE)
465 d_set_d_op(dentry, &smbfs_dentry_operations_case);
467 d_set_d_op(dentry, &smbfs_dentry_operations);
469 d_add(dentry, inode);
470 smb_renew_times(dentry);
476 return ERR_PTR(error);
480 * This code is common to all routines creating a new inode.
483 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
485 struct smb_sb_info *server = server_from_dentry(dentry);
488 struct smb_fattr fattr;
490 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
492 error = smb_proc_getattr(dentry, &fattr);
496 smb_renew_times(dentry);
497 fattr.f_ino = iunique(dentry->d_sb, 2);
498 inode = smb_iget(dentry->d_sb, &fattr);
503 struct smb_inode_info *ei = SMB_I(inode);
505 ei->access = SMB_O_RDWR;
506 ei->open = server->generation;
508 d_instantiate(dentry, inode);
516 PARANOIA("%s/%s failed, error=%d, closing %u\n",
517 DENTRY_PATH(dentry), error, fileid);
518 smb_close_fileid(dentry, fileid);
523 /* N.B. How should the mode argument be used? */
525 smb_create(struct inode *dir, struct dentry *dentry, int mode,
526 struct nameidata *nd)
528 struct smb_sb_info *server = server_from_dentry(dentry);
533 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
536 smb_invalid_dir_cache(dir);
537 error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
539 if (server->opt.capabilities & SMB_CAP_UNIX) {
540 /* Set attributes for new file */
541 attr.ia_valid = ATTR_MODE;
543 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
545 error = smb_instantiate(dentry, fileid, 1);
547 PARANOIA("%s/%s failed, error=%d\n",
548 DENTRY_PATH(dentry), error);
554 /* N.B. How should the mode argument be used? */
556 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
558 struct smb_sb_info *server = server_from_dentry(dentry);
563 smb_invalid_dir_cache(dir);
564 error = smb_proc_mkdir(dentry);
566 if (server->opt.capabilities & SMB_CAP_UNIX) {
567 /* Set attributes for new directory */
568 attr.ia_valid = ATTR_MODE;
570 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
572 error = smb_instantiate(dentry, 0, 0);
579 smb_rmdir(struct inode *dir, struct dentry *dentry)
581 struct inode *inode = dentry->d_inode;
585 * Close the directory if it's open.
591 * Check that nobody else is using the directory..
594 if (!d_unhashed(dentry))
597 smb_invalid_dir_cache(dir);
598 error = smb_proc_rmdir(dentry);
606 smb_unlink(struct inode *dir, struct dentry *dentry)
611 * Close the file if it's open.
614 smb_close(dentry->d_inode);
616 smb_invalid_dir_cache(dir);
617 error = smb_proc_unlink(dentry);
619 smb_renew_times(dentry);
625 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
626 struct inode *new_dir, struct dentry *new_dentry)
631 * Close any open files, and check whether to delete the
632 * target before attempting the rename.
635 if (old_dentry->d_inode)
636 smb_close(old_dentry->d_inode);
637 if (new_dentry->d_inode) {
638 smb_close(new_dentry->d_inode);
639 error = smb_proc_unlink(new_dentry);
641 VERBOSE("unlink %s/%s, error=%d\n",
642 DENTRY_PATH(new_dentry), error);
646 d_delete(new_dentry);
649 smb_invalid_dir_cache(old_dir);
650 smb_invalid_dir_cache(new_dir);
651 error = smb_proc_mv(old_dentry, new_dentry);
653 smb_renew_times(old_dentry);
654 smb_renew_times(new_dentry);
662 * FIXME: samba servers won't let you create device nodes unless uid/gid
663 * matches the connection credentials (and we don't know which those are ...)
666 smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
671 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
673 current_euid_egid(&attr.ia_uid, &attr.ia_gid);
675 if (!new_valid_dev(dev))
678 smb_invalid_dir_cache(dir);
679 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
681 error = smb_instantiate(dentry, 0, 0);
687 * dentry = existing file
688 * new_dentry = new file
691 smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
695 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
696 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
697 smb_invalid_dir_cache(dir);
698 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
700 smb_renew_times(dentry);
701 error = smb_instantiate(new_dentry, 0, 0);