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 *);
34 struct file_operations smb_dir_operations =
36 read: generic_read_dir,
42 struct inode_operations smb_dir_inode_operations =
50 revalidate: smb_revalidate_inode,
51 setattr: smb_notify_change,
55 * Read a directory, using filldir to fill the dirent memory.
56 * smb_proc_readdir does the actual reading from the smb server.
58 * The cache code is almost directly taken from ncpfs
61 smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
63 struct dentry *dentry = filp->f_dentry;
64 struct inode *dir = dentry->d_inode;
65 struct smb_sb_info *server = server_from_dentry(dentry);
66 union smb_dir_cache *cache = NULL;
67 struct smb_cache_control ctl;
68 struct page *page = NULL;
74 VERBOSE("reading %s/%s, f_pos=%d\n",
75 DENTRY_PATH(dentry), (int) filp->f_pos);
78 switch ((unsigned int) filp->f_pos) {
80 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
85 if (filldir(dirent, "..", 2, 1,
86 dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
92 * Make sure our inode is up-to-date.
94 result = smb_revalidate_inode(dentry);
99 page = grab_cache_page(&dir->i_data, 0);
103 ctl.cache = cache = kmap(page);
104 ctl.head = cache->head;
106 if (!Page_Uptodate(page) || !ctl.head.eof) {
107 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
108 DENTRY_PATH(dentry), Page_Uptodate(page),ctl.head.eof);
112 if (filp->f_pos == 2) {
113 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
117 * N.B. ncpfs checks mtime of dentry too here, we don't.
118 * 1. common smb servers do not update mtime on dir changes
119 * 2. it requires an extra smb request
120 * (revalidate has the same timeout as ctl.head.time)
122 * Instead smbfs invalidates its own cache on local changes
123 * and remote changes are not seen until timeout.
127 if (filp->f_pos > ctl.head.end)
130 ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
131 ctl.ofs = ctl.fpos / SMB_DIRCACHE_SIZE;
132 ctl.idx = ctl.fpos % SMB_DIRCACHE_SIZE;
136 ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
139 ctl.cache = kmap(ctl.page);
140 if (!Page_Uptodate(ctl.page))
143 while (ctl.idx < SMB_DIRCACHE_SIZE) {
147 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
148 dentry, filp->f_pos);
152 res = filldir(dirent, dent->d_name.name,
153 dent->d_name.len, filp->f_pos,
154 dent->d_inode->i_ino, DT_UNKNOWN);
160 if (filp->f_pos > ctl.head.end)
165 SetPageUptodate(ctl.page);
166 UnlockPage(ctl.page);
167 page_cache_release(ctl.page);
176 UnlockPage(ctl.page);
177 page_cache_release(ctl.page);
182 smb_invalidate_dircache_entries(dentry);
183 ctl.head.time = jiffies;
187 ctl.idx = SMB_DIRCACHE_START;
191 result = smb_proc_readdir(filp, dirent, filldir, &ctl);
193 goto invalid_cache; /* retry */
194 ctl.head.end = ctl.fpos - 1;
195 ctl.head.eof = ctl.valid;
198 cache->head = ctl.head;
200 SetPageUptodate(page);
202 page_cache_release(page);
206 SetPageUptodate(ctl.page);
207 UnlockPage(ctl.page);
208 page_cache_release(ctl.page);
215 smb_dir_open(struct inode *dir, struct file *file)
217 struct dentry *dentry = file->f_dentry;
218 struct smb_sb_info *server;
221 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
222 file->f_dentry->d_name.name);
225 * Directory timestamps in the core protocol aren't updated
226 * when a file is added, so we give them a very short TTL.
229 server = server_from_dentry(dentry);
230 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
231 unsigned long age = jiffies - dir->u.smbfs_i.oldmtime;
233 smb_invalid_dir_cache(dir);
237 * Note: in order to allow the smbmount process to open the
238 * mount point, we only revalidate if the connection is valid or
239 * if the process is trying to access something other than the root.
241 if (server->state == CONN_VALID || !IS_ROOT(dentry))
242 error = smb_revalidate_inode(dentry);
248 * Dentry operations routines
250 static int smb_lookup_validate(struct dentry *, int);
251 static int smb_hash_dentry(struct dentry *, struct qstr *);
252 static int smb_compare_dentry(struct dentry *, struct qstr *, struct qstr *);
253 static int smb_delete_dentry(struct dentry *);
255 static struct dentry_operations smbfs_dentry_operations =
257 d_revalidate: smb_lookup_validate,
258 d_hash: smb_hash_dentry,
259 d_compare: smb_compare_dentry,
260 d_delete: smb_delete_dentry,
263 static struct dentry_operations smbfs_dentry_operations_case =
265 d_revalidate: smb_lookup_validate,
266 d_delete: smb_delete_dentry,
271 * This is the callback when the dcache has a lookup hit.
274 smb_lookup_validate(struct dentry * dentry, int flags)
276 struct smb_sb_info *server = server_from_dentry(dentry);
277 struct inode * inode = dentry->d_inode;
278 unsigned long age = jiffies - dentry->d_time;
282 * The default validation is based on dentry age:
283 * we believe in dentries for a few seconds. (But each
284 * successful server lookup renews the timestamp.)
286 valid = (age <= SMB_MAX_AGE(server));
287 #ifdef SMBFS_DEBUG_VERBOSE
289 VERBOSE("%s/%s not valid, age=%lu\n",
290 DENTRY_PATH(dentry), age);
295 if (is_bad_inode(inode)) {
296 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
299 valid = (smb_revalidate_inode(dentry) == 0);
303 * What should we do for negative dentries?
310 smb_hash_dentry(struct dentry *dir, struct qstr *this)
315 hash = init_name_hash();
316 for (i=0; i < this->len ; i++)
317 hash = partial_name_hash(tolower(this->name[i]), hash);
318 this->hash = end_name_hash(hash);
324 smb_compare_dentry(struct dentry *dir, struct qstr *a, struct qstr *b)
328 if (a->len != b->len)
330 for (i=0; i < a->len; i++) {
331 if (tolower(a->name[i]) != tolower(b->name[i]))
340 * This is the callback from dput() when d_count is going to 0.
341 * We use this to unhash dentries with bad inodes.
344 smb_delete_dentry(struct dentry * dentry)
346 if (dentry->d_inode) {
347 if (is_bad_inode(dentry->d_inode)) {
348 PARANOIA("bad inode, unhashing %s/%s\n",
349 DENTRY_PATH(dentry));
353 /* N.B. Unhash negative dentries? */
359 * Initialize a new dentry
362 smb_new_dentry(struct dentry *dentry)
364 struct smb_sb_info *server = server_from_dentry(dentry);
366 if (server->mnt->flags & SMB_MOUNT_CASE)
367 dentry->d_op = &smbfs_dentry_operations_case;
369 dentry->d_op = &smbfs_dentry_operations;
370 dentry->d_time = jiffies;
375 * Whenever a lookup succeeds, we know the parent directories
376 * are all valid, so we want to update the dentry timestamps.
377 * N.B. Move this to dcache?
380 smb_renew_times(struct dentry * dentry)
383 dentry->d_time = jiffies;
386 dentry = dentry->d_parent;
390 static struct dentry *
391 smb_lookup(struct inode *dir, struct dentry *dentry)
393 struct smb_fattr finfo;
396 struct smb_sb_info *server;
398 error = -ENAMETOOLONG;
399 if (dentry->d_name.len > SMB_MAXNAMELEN)
402 error = smb_proc_getattr(dentry, &finfo);
403 #ifdef SMBFS_PARANOIA
404 if (error && error != -ENOENT)
405 PARANOIA("find %s/%s failed, error=%d\n",
406 DENTRY_PATH(dentry), error);
410 if (error == -ENOENT)
414 finfo.f_ino = iunique(dentry->d_sb, 2);
415 inode = smb_iget(dir->i_sb, &finfo);
418 server = server_from_dentry(dentry);
419 if (server->mnt->flags & SMB_MOUNT_CASE)
420 dentry->d_op = &smbfs_dentry_operations_case;
422 dentry->d_op = &smbfs_dentry_operations;
424 d_add(dentry, inode);
425 smb_renew_times(dentry);
430 return ERR_PTR(error);
434 * This code is common to all routines creating a new inode.
437 smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
439 struct smb_sb_info *server = server_from_dentry(dentry);
442 struct smb_fattr fattr;
444 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
446 error = smb_proc_getattr(dentry, &fattr);
450 smb_renew_times(dentry);
451 fattr.f_ino = iunique(dentry->d_sb, 2);
452 inode = smb_iget(dentry->d_sb, &fattr);
457 inode->u.smbfs_i.fileid = fileid;
458 inode->u.smbfs_i.access = SMB_O_RDWR;
459 inode->u.smbfs_i.open = server->generation;
461 d_instantiate(dentry, inode);
469 PARANOIA("%s/%s failed, error=%d, closing %u\n",
470 DENTRY_PATH(dentry), error, fileid);
471 smb_close_fileid(dentry, fileid);
476 /* N.B. How should the mode argument be used? */
478 smb_create(struct inode *dir, struct dentry *dentry, int mode)
483 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
485 smb_invalid_dir_cache(dir);
486 error = smb_proc_create(dentry, 0, CURRENT_TIME, &fileid);
488 error = smb_instantiate(dentry, fileid, 1);
490 PARANOIA("%s/%s failed, error=%d\n",
491 DENTRY_PATH(dentry), error);
496 /* N.B. How should the mode argument be used? */
498 smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
502 smb_invalid_dir_cache(dir);
503 error = smb_proc_mkdir(dentry);
505 error = smb_instantiate(dentry, 0, 0);
511 smb_rmdir(struct inode *dir, struct dentry *dentry)
513 struct inode *inode = dentry->d_inode;
517 * Close the directory if it's open.
522 * Check that nobody else is using the directory..
525 if (!d_unhashed(dentry))
528 smb_invalid_dir_cache(dir);
529 error = smb_proc_rmdir(dentry);
536 smb_unlink(struct inode *dir, struct dentry *dentry)
541 * Close the file if it's open.
543 smb_close(dentry->d_inode);
545 smb_invalid_dir_cache(dir);
546 error = smb_proc_unlink(dentry);
548 smb_renew_times(dentry);
553 smb_rename(struct inode *old_dir, struct dentry *old_dentry,
554 struct inode *new_dir, struct dentry *new_dentry)
559 * Close any open files, and check whether to delete the
560 * target before attempting the rename.
562 if (old_dentry->d_inode)
563 smb_close(old_dentry->d_inode);
564 if (new_dentry->d_inode) {
565 smb_close(new_dentry->d_inode);
566 error = smb_proc_unlink(new_dentry);
568 VERBOSE("unlink %s/%s, error=%d\n",
569 DENTRY_PATH(new_dentry), error);
573 d_delete(new_dentry);
576 smb_invalid_dir_cache(old_dir);
577 smb_invalid_dir_cache(new_dir);
578 error = smb_proc_mv(old_dentry, new_dentry);
580 smb_renew_times(old_dentry);
581 smb_renew_times(new_dentry);