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"
22 #define SMBFS_MAX_AGE 5*HZ
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
*);
28 static int smb_create(struct inode
*, struct dentry
*, int);
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
*);
35 struct file_operations smb_dir_operations
=
37 read
: generic_read_dir
,
43 struct inode_operations smb_dir_inode_operations
=
51 revalidate
: smb_revalidate_inode
,
52 setattr
: smb_notify_change
,
56 smb_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
58 struct dentry
*dentry
= filp
->f_dentry
;
59 struct inode
*dir
= dentry
->d_inode
;
60 struct cache_head
*cachep
;
63 VERBOSE("reading %s/%s, f_pos=%d\n",
64 DENTRY_PATH(dentry
), (int) filp
->f_pos
);
67 switch ((unsigned int) filp
->f_pos
)
70 if (filldir(dirent
, ".", 1, 0, dir
->i_ino
, DT_DIR
) < 0)
74 if (filldir(dirent
, "..", 2, 1,
75 dentry
->d_parent
->d_inode
->i_ino
, DT_DIR
) < 0)
81 * Make sure our inode is up-to-date.
83 result
= smb_revalidate_inode(dentry
);
87 * Get the cache pointer ...
90 cachep
= smb_get_dircache(dentry
);
95 * Make sure the cache is up-to-date.
97 * To detect changes on the server we refill on each "new" access.
99 * Directory mtime would be nice to use for finding changes,
100 * unfortunately some servers (NT4) doesn't update on local changes.
102 if (!cachep
->valid
|| filp
->f_pos
== 2)
104 result
= smb_refill_dircache(cachep
, dentry
);
113 struct cache_dirent this_dirent
, *entry
= &this_dirent
;
115 if (!smb_find_in_cache(cachep
, filp
->f_pos
, entry
))
118 * Check whether to look up the inode number.
122 /* N.B. Make cache_dirent name a qstr! */
123 qname
.name
= entry
->name
;
124 qname
.len
= entry
->len
;
125 entry
->ino
= find_inode_number(dentry
, &qname
);
127 entry
->ino
= iunique(dentry
->d_sb
, 2);
130 if (filldir(dirent
, entry
->name
, entry
->len
,
131 filp
->f_pos
, entry
->ino
, DT_UNKNOWN
) < 0)
137 * Release the dircache.
140 smb_free_dircache(cachep
);
146 * Note: in order to allow the smbmount process to open the
147 * mount point, we don't revalidate if conn_pid is NULL.
150 smb_dir_open(struct inode
*dir
, struct file
*file
)
152 struct dentry
*dentry
= file
->f_dentry
;
153 struct smb_sb_info
*server
;
156 VERBOSE("(%s/%s)\n", dentry
->d_parent
->d_name
.name
,
157 file
->f_dentry
->d_name
.name
);
160 * Directory timestamps in the core protocol aren't updated
161 * when a file is added, so we give them a very short TTL.
164 server
= server_from_dentry(dentry
);
165 if (server
->opt
.protocol
< SMB_PROTOCOL_LANMAN2
)
167 unsigned long age
= jiffies
- dir
->u
.smbfs_i
.oldmtime
;
169 smb_invalid_dir_cache(dir
);
172 if (server
->conn_pid
)
173 error
= smb_revalidate_inode(dentry
);
179 * Dentry operations routines
181 static int smb_lookup_validate(struct dentry
*, int);
182 static int smb_hash_dentry(struct dentry
*, struct qstr
*);
183 static int smb_compare_dentry(struct dentry
*, struct qstr
*, struct qstr
*);
184 static int smb_delete_dentry(struct dentry
*);
186 static struct dentry_operations smbfs_dentry_operations
=
188 d_revalidate
: smb_lookup_validate
,
189 d_hash
: smb_hash_dentry
,
190 d_compare
: smb_compare_dentry
,
191 d_delete
: smb_delete_dentry
,
194 static struct dentry_operations smbfs_dentry_operations_case
=
196 d_revalidate
: smb_lookup_validate
,
197 d_delete
: smb_delete_dentry
,
202 * This is the callback when the dcache has a lookup hit.
205 smb_lookup_validate(struct dentry
* dentry
, int flags
)
207 struct inode
* inode
= dentry
->d_inode
;
208 unsigned long age
= jiffies
- dentry
->d_time
;
212 * The default validation is based on dentry age:
213 * we believe in dentries for 5 seconds. (But each
214 * successful server lookup renews the timestamp.)
216 valid
= (age
<= SMBFS_MAX_AGE
);
217 #ifdef SMBFS_DEBUG_VERBOSE
219 VERBOSE("%s/%s not valid, age=%lu\n",
220 DENTRY_PATH(dentry
), age
);
225 if (is_bad_inode(inode
)) {
226 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry
));
229 valid
= (smb_revalidate_inode(dentry
) == 0);
233 * What should we do for negative dentries?
240 smb_hash_dentry(struct dentry
*dir
, struct qstr
*this)
245 hash
= init_name_hash();
246 for (i
=0; i
< this->len
; i
++)
247 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
248 this->hash
= end_name_hash(hash
);
254 smb_compare_dentry(struct dentry
*dir
, struct qstr
*a
, struct qstr
*b
)
258 if (a
->len
!= b
->len
)
260 for (i
=0; i
< a
->len
; i
++) {
261 if (tolower(a
->name
[i
]) != tolower(b
->name
[i
]))
270 * This is the callback from dput() when d_count is going to 0.
271 * We use this to unhash dentries with bad inodes.
274 smb_delete_dentry(struct dentry
* dentry
)
276 if (dentry
->d_inode
) {
277 if (is_bad_inode(dentry
->d_inode
)) {
278 PARANOIA("bad inode, unhashing %s/%s\n",
279 DENTRY_PATH(dentry
));
283 /* N.B. Unhash negative dentries? */
289 * Whenever a lookup succeeds, we know the parent directories
290 * are all valid, so we want to update the dentry timestamps.
291 * N.B. Move this to dcache?
294 smb_renew_times(struct dentry
* dentry
)
297 dentry
->d_time
= jiffies
;
300 dentry
= dentry
->d_parent
;
304 static struct dentry
*
305 smb_lookup(struct inode
*dir
, struct dentry
*dentry
)
307 struct smb_fattr finfo
;
310 struct smb_sb_info
*server
;
312 error
= -ENAMETOOLONG
;
313 if (dentry
->d_name
.len
> SMB_MAXNAMELEN
)
316 error
= smb_proc_getattr(dentry
, &finfo
);
317 #ifdef SMBFS_PARANOIA
318 if (error
&& error
!= -ENOENT
)
319 PARANOIA("find %s/%s failed, error=%d\n",
320 DENTRY_PATH(dentry
), error
);
324 if (error
== -ENOENT
)
328 finfo
.f_ino
= iunique(dentry
->d_sb
, 2);
329 inode
= smb_iget(dir
->i_sb
, &finfo
);
332 server
= server_from_dentry(dentry
);
333 if (server
->mnt
->flags
& SMB_MOUNT_CASE
)
334 dentry
->d_op
= &smbfs_dentry_operations_case
;
336 dentry
->d_op
= &smbfs_dentry_operations
;
338 d_add(dentry
, inode
);
339 smb_renew_times(dentry
);
344 return ERR_PTR(error
);
348 * This code is common to all routines creating a new inode.
351 smb_instantiate(struct dentry
*dentry
, __u16 fileid
, int have_id
)
353 struct smb_sb_info
*server
= server_from_dentry(dentry
);
356 struct smb_fattr fattr
;
358 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry
), fileid
);
360 error
= smb_proc_getattr(dentry
, &fattr
);
364 smb_renew_times(dentry
);
365 fattr
.f_ino
= iunique(dentry
->d_sb
, 2);
366 inode
= smb_iget(dentry
->d_sb
, &fattr
);
372 inode
->u
.smbfs_i
.fileid
= fileid
;
373 inode
->u
.smbfs_i
.access
= SMB_O_RDWR
;
374 inode
->u
.smbfs_i
.open
= server
->generation
;
376 d_instantiate(dentry
, inode
);
385 PARANOIA("%s/%s failed, error=%d, closing %u\n",
386 DENTRY_PATH(dentry
), error
, fileid
);
387 smb_close_fileid(dentry
, fileid
);
392 /* N.B. How should the mode argument be used? */
394 smb_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
399 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry
), mode
);
401 smb_invalid_dir_cache(dir
);
402 error
= smb_proc_create(dentry
, 0, CURRENT_TIME
, &fileid
);
404 error
= smb_instantiate(dentry
, fileid
, 1);
406 PARANOIA("%s/%s failed, error=%d\n",
407 DENTRY_PATH(dentry
), error
);
412 /* N.B. How should the mode argument be used? */
414 smb_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
418 smb_invalid_dir_cache(dir
);
419 error
= smb_proc_mkdir(dentry
);
421 error
= smb_instantiate(dentry
, 0, 0);
427 smb_rmdir(struct inode
*dir
, struct dentry
*dentry
)
429 struct inode
*inode
= dentry
->d_inode
;
433 * Close the directory if it's open.
438 * Check that nobody else is using the directory..
441 if (!d_unhashed(dentry
))
444 error
= smb_proc_rmdir(dentry
);
451 smb_unlink(struct inode
*dir
, struct dentry
*dentry
)
456 * Close the file if it's open.
458 smb_close(dentry
->d_inode
);
460 error
= smb_proc_unlink(dentry
);
462 smb_renew_times(dentry
);
467 smb_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
468 struct inode
*new_dir
, struct dentry
*new_dentry
)
473 * Close any open files, and check whether to delete the
474 * target before attempting the rename.
476 if (old_dentry
->d_inode
)
477 smb_close(old_dentry
->d_inode
);
478 if (new_dentry
->d_inode
)
480 smb_close(new_dentry
->d_inode
);
481 error
= smb_proc_unlink(new_dentry
);
484 VERBOSE("unlink %s/%s, error=%d\n",
485 DENTRY_PATH(new_dentry
), error
);
489 d_delete(new_dentry
);
492 smb_invalid_dir_cache(old_dir
);
493 smb_invalid_dir_cache(new_dir
);
494 error
= smb_proc_mv(old_dentry
, new_dentry
);
497 smb_renew_times(old_dentry
);
498 smb_renew_times(new_dentry
);