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>
17 #include <linux/namei.h>
20 #include "smb_mount.h"
23 #include "smb_debug.h"
26 static int smb_readdir(struct file
*, void *, filldir_t
);
27 static int smb_dir_open(struct inode
*, struct file
*);
29 static struct dentry
*smb_lookup(struct inode
*, struct dentry
*, struct nameidata
*);
30 static int smb_create(struct inode
*, struct dentry
*, int, struct nameidata
*);
31 static int smb_mkdir(struct inode
*, struct dentry
*, int);
32 static int smb_rmdir(struct inode
*, struct dentry
*);
33 static int smb_unlink(struct inode
*, struct dentry
*);
34 static int smb_rename(struct inode
*, struct dentry
*,
35 struct inode
*, struct dentry
*);
36 static int smb_make_node(struct inode
*,struct dentry
*,int,dev_t
);
37 static int smb_link(struct dentry
*, struct inode
*, struct dentry
*);
39 const struct file_operations smb_dir_operations
=
41 .llseek
= generic_file_llseek
,
42 .read
= generic_read_dir
,
43 .readdir
= smb_readdir
,
44 .unlocked_ioctl
= smb_ioctl
,
48 const struct inode_operations smb_dir_inode_operations
=
56 .getattr
= smb_getattr
,
57 .setattr
= smb_notify_change
,
60 const struct inode_operations smb_dir_inode_operations_unix
=
68 .getattr
= smb_getattr
,
69 .setattr
= smb_notify_change
,
70 .symlink
= smb_symlink
,
71 .mknod
= smb_make_node
,
76 * Read a directory, using filldir to fill the dirent memory.
77 * smb_proc_readdir does the actual reading from the smb server.
79 * The cache code is almost directly taken from ncpfs
82 smb_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
84 struct dentry
*dentry
= filp
->f_path
.dentry
;
85 struct inode
*dir
= dentry
->d_inode
;
86 struct smb_sb_info
*server
= server_from_dentry(dentry
);
87 union smb_dir_cache
*cache
= NULL
;
88 struct smb_cache_control ctl
;
89 struct page
*page
= NULL
;
95 VERBOSE("reading %s/%s, f_pos=%d\n",
96 DENTRY_PATH(dentry
), (int) filp
->f_pos
);
102 switch ((unsigned int) filp
->f_pos
) {
104 if (filldir(dirent
, ".", 1, 0, dir
->i_ino
, DT_DIR
) < 0)
109 if (filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
) < 0)
115 * Make sure our inode is up-to-date.
117 result
= smb_revalidate_inode(dentry
);
122 page
= grab_cache_page(&dir
->i_data
, 0);
126 ctl
.cache
= cache
= kmap(page
);
127 ctl
.head
= cache
->head
;
129 if (!PageUptodate(page
) || !ctl
.head
.eof
) {
130 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
131 DENTRY_PATH(dentry
), PageUptodate(page
),ctl
.head
.eof
);
135 if (filp
->f_pos
== 2) {
136 if (jiffies
- ctl
.head
.time
>= SMB_MAX_AGE(server
))
140 * N.B. ncpfs checks mtime of dentry too here, we don't.
141 * 1. common smb servers do not update mtime on dir changes
142 * 2. it requires an extra smb request
143 * (revalidate has the same timeout as ctl.head.time)
145 * Instead smbfs invalidates its own cache on local changes
146 * and remote changes are not seen until timeout.
150 if (filp
->f_pos
> ctl
.head
.end
)
153 ctl
.fpos
= filp
->f_pos
+ (SMB_DIRCACHE_START
- 2);
154 ctl
.ofs
= ctl
.fpos
/ SMB_DIRCACHE_SIZE
;
155 ctl
.idx
= ctl
.fpos
% SMB_DIRCACHE_SIZE
;
159 ctl
.page
= find_lock_page(&dir
->i_data
, ctl
.ofs
);
162 ctl
.cache
= kmap(ctl
.page
);
163 if (!PageUptodate(ctl
.page
))
166 while (ctl
.idx
< SMB_DIRCACHE_SIZE
) {
170 dent
= smb_dget_fpos(ctl
.cache
->dentry
[ctl
.idx
],
171 dentry
, filp
->f_pos
);
175 res
= filldir(dirent
, dent
->d_name
.name
,
176 dent
->d_name
.len
, filp
->f_pos
,
177 dent
->d_inode
->i_ino
, DT_UNKNOWN
);
183 if (filp
->f_pos
> ctl
.head
.end
)
188 SetPageUptodate(ctl
.page
);
189 unlock_page(ctl
.page
);
190 page_cache_release(ctl
.page
);
199 unlock_page(ctl
.page
);
200 page_cache_release(ctl
.page
);
205 smb_invalidate_dircache_entries(dentry
);
206 ctl
.head
.time
= jiffies
;
210 ctl
.idx
= SMB_DIRCACHE_START
;
214 result
= server
->ops
->readdir(filp
, dirent
, filldir
, &ctl
);
215 if (result
== -ERESTARTSYS
&& page
)
216 ClearPageUptodate(page
);
218 goto invalid_cache
; /* retry */
219 ctl
.head
.end
= ctl
.fpos
- 1;
220 ctl
.head
.eof
= ctl
.valid
;
223 cache
->head
= ctl
.head
;
225 if (result
!= -ERESTARTSYS
)
226 SetPageUptodate(page
);
228 page_cache_release(page
);
232 SetPageUptodate(ctl
.page
);
233 unlock_page(ctl
.page
);
234 page_cache_release(ctl
.page
);
242 smb_dir_open(struct inode
*dir
, struct file
*file
)
244 struct dentry
*dentry
= file
->f_path
.dentry
;
245 struct smb_sb_info
*server
;
248 VERBOSE("(%s/%s)\n", dentry
->d_parent
->d_name
.name
,
249 file
->f_path
.dentry
->d_name
.name
);
252 * Directory timestamps in the core protocol aren't updated
253 * when a file is added, so we give them a very short TTL.
256 server
= server_from_dentry(dentry
);
257 if (server
->opt
.protocol
< SMB_PROTOCOL_LANMAN2
) {
258 unsigned long age
= jiffies
- SMB_I(dir
)->oldmtime
;
260 smb_invalid_dir_cache(dir
);
264 * Note: in order to allow the smbmount process to open the
265 * mount point, we only revalidate if the connection is valid or
266 * if the process is trying to access something other than the root.
268 if (server
->state
== CONN_VALID
|| !IS_ROOT(dentry
))
269 error
= smb_revalidate_inode(dentry
);
275 * Dentry operations routines
277 static int smb_lookup_validate(struct dentry
*, struct nameidata
*);
278 static int smb_hash_dentry(const struct dentry
*, const struct inode
*,
280 static int smb_compare_dentry(const struct dentry
*,
281 const struct inode
*,
282 const struct dentry
*, const struct inode
*,
283 unsigned int, const char *, const struct qstr
*);
284 static int smb_delete_dentry(const struct dentry
*);
286 static const struct dentry_operations smbfs_dentry_operations
=
288 .d_revalidate
= smb_lookup_validate
,
289 .d_hash
= smb_hash_dentry
,
290 .d_compare
= smb_compare_dentry
,
291 .d_delete
= smb_delete_dentry
,
294 static const struct dentry_operations smbfs_dentry_operations_case
=
296 .d_revalidate
= smb_lookup_validate
,
297 .d_delete
= smb_delete_dentry
,
302 * This is the callback when the dcache has a lookup hit.
305 smb_lookup_validate(struct dentry
*dentry
, struct nameidata
*nd
)
307 struct smb_sb_info
*server
;
312 if (nd
->flags
& LOOKUP_RCU
)
315 server
= server_from_dentry(dentry
);
316 inode
= dentry
->d_inode
;
317 age
= jiffies
- dentry
->d_time
;
320 * The default validation is based on dentry age:
321 * we believe in dentries for a few seconds. (But each
322 * successful server lookup renews the timestamp.)
324 valid
= (age
<= SMB_MAX_AGE(server
));
325 #ifdef SMBFS_DEBUG_VERBOSE
327 VERBOSE("%s/%s not valid, age=%lu\n",
328 DENTRY_PATH(dentry
), age
);
333 if (is_bad_inode(inode
)) {
334 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry
));
337 valid
= (smb_revalidate_inode(dentry
) == 0);
341 * What should we do for negative dentries?
348 smb_hash_dentry(const struct dentry
*dir
, const struct inode
*inode
,
354 hash
= init_name_hash();
355 for (i
=0; i
< this->len
; i
++)
356 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
357 this->hash
= end_name_hash(hash
);
363 smb_compare_dentry(const struct dentry
*parent
,
364 const struct inode
*pinode
,
365 const struct dentry
*dentry
, const struct inode
*inode
,
366 unsigned int len
, const char *str
, const struct qstr
*name
)
370 if (len
!= name
->len
)
372 for (i
=0; i
< len
; i
++) {
373 if (tolower(str
[i
]) != tolower(name
->name
[i
]))
382 * This is the callback from dput() when d_count is going to 0.
383 * We use this to unhash dentries with bad inodes.
386 smb_delete_dentry(const struct dentry
*dentry
)
388 if (dentry
->d_inode
) {
389 if (is_bad_inode(dentry
->d_inode
)) {
390 PARANOIA("bad inode, unhashing %s/%s\n",
391 DENTRY_PATH(dentry
));
395 /* N.B. Unhash negative dentries? */
401 * Initialize a new dentry
404 smb_new_dentry(struct dentry
*dentry
)
406 dentry
->d_time
= jiffies
;
411 * Whenever a lookup succeeds, we know the parent directories
412 * are all valid, so we want to update the dentry timestamps.
413 * N.B. Move this to dcache?
416 smb_renew_times(struct dentry
* dentry
)
419 dentry
->d_time
= jiffies
;
421 while (!IS_ROOT(dentry
)) {
422 struct dentry
*parent
= dget_parent(dentry
);
426 dentry
->d_time
= jiffies
;
431 static struct dentry
*
432 smb_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
434 struct smb_fattr finfo
;
438 error
= -ENAMETOOLONG
;
439 if (dentry
->d_name
.len
> SMB_MAXNAMELEN
)
442 /* Do not allow lookup of names with backslashes in */
444 if (memchr(dentry
->d_name
.name
, '\\', dentry
->d_name
.len
))
448 error
= smb_proc_getattr(dentry
, &finfo
);
449 #ifdef SMBFS_PARANOIA
450 if (error
&& error
!= -ENOENT
)
451 PARANOIA("find %s/%s failed, error=%d\n",
452 DENTRY_PATH(dentry
), error
);
456 if (error
== -ENOENT
)
460 finfo
.f_ino
= iunique(dentry
->d_sb
, 2);
461 inode
= smb_iget(dir
->i_sb
, &finfo
);
464 d_add(dentry
, inode
);
465 smb_renew_times(dentry
);
471 return ERR_PTR(error
);
475 * This code is common to all routines creating a new inode.
478 smb_instantiate(struct dentry
*dentry
, __u16 fileid
, int have_id
)
480 struct smb_sb_info
*server
= server_from_dentry(dentry
);
483 struct smb_fattr fattr
;
485 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry
), fileid
);
487 error
= smb_proc_getattr(dentry
, &fattr
);
491 smb_renew_times(dentry
);
492 fattr
.f_ino
= iunique(dentry
->d_sb
, 2);
493 inode
= smb_iget(dentry
->d_sb
, &fattr
);
498 struct smb_inode_info
*ei
= SMB_I(inode
);
500 ei
->access
= SMB_O_RDWR
;
501 ei
->open
= server
->generation
;
503 d_instantiate(dentry
, inode
);
511 PARANOIA("%s/%s failed, error=%d, closing %u\n",
512 DENTRY_PATH(dentry
), error
, fileid
);
513 smb_close_fileid(dentry
, fileid
);
518 /* N.B. How should the mode argument be used? */
520 smb_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
521 struct nameidata
*nd
)
523 struct smb_sb_info
*server
= server_from_dentry(dentry
);
528 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry
), mode
);
531 smb_invalid_dir_cache(dir
);
532 error
= smb_proc_create(dentry
, 0, get_seconds(), &fileid
);
534 if (server
->opt
.capabilities
& SMB_CAP_UNIX
) {
535 /* Set attributes for new file */
536 attr
.ia_valid
= ATTR_MODE
;
538 error
= smb_proc_setattr_unix(dentry
, &attr
, 0, 0);
540 error
= smb_instantiate(dentry
, fileid
, 1);
542 PARANOIA("%s/%s failed, error=%d\n",
543 DENTRY_PATH(dentry
), error
);
549 /* N.B. How should the mode argument be used? */
551 smb_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
553 struct smb_sb_info
*server
= server_from_dentry(dentry
);
558 smb_invalid_dir_cache(dir
);
559 error
= smb_proc_mkdir(dentry
);
561 if (server
->opt
.capabilities
& SMB_CAP_UNIX
) {
562 /* Set attributes for new directory */
563 attr
.ia_valid
= ATTR_MODE
;
565 error
= smb_proc_setattr_unix(dentry
, &attr
, 0, 0);
567 error
= smb_instantiate(dentry
, 0, 0);
574 smb_rmdir(struct inode
*dir
, struct dentry
*dentry
)
576 struct inode
*inode
= dentry
->d_inode
;
580 * Close the directory if it's open.
586 * Check that nobody else is using the directory..
589 if (!d_unhashed(dentry
))
592 smb_invalid_dir_cache(dir
);
593 error
= smb_proc_rmdir(dentry
);
601 smb_unlink(struct inode
*dir
, struct dentry
*dentry
)
606 * Close the file if it's open.
609 smb_close(dentry
->d_inode
);
611 smb_invalid_dir_cache(dir
);
612 error
= smb_proc_unlink(dentry
);
614 smb_renew_times(dentry
);
620 smb_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
621 struct inode
*new_dir
, struct dentry
*new_dentry
)
626 * Close any open files, and check whether to delete the
627 * target before attempting the rename.
630 if (old_dentry
->d_inode
)
631 smb_close(old_dentry
->d_inode
);
632 if (new_dentry
->d_inode
) {
633 smb_close(new_dentry
->d_inode
);
634 error
= smb_proc_unlink(new_dentry
);
636 VERBOSE("unlink %s/%s, error=%d\n",
637 DENTRY_PATH(new_dentry
), error
);
641 d_delete(new_dentry
);
644 smb_invalid_dir_cache(old_dir
);
645 smb_invalid_dir_cache(new_dir
);
646 error
= smb_proc_mv(old_dentry
, new_dentry
);
648 smb_renew_times(old_dentry
);
649 smb_renew_times(new_dentry
);
657 * FIXME: samba servers won't let you create device nodes unless uid/gid
658 * matches the connection credentials (and we don't know which those are ...)
661 smb_make_node(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
666 attr
.ia_valid
= ATTR_MODE
| ATTR_UID
| ATTR_GID
;
668 current_euid_egid(&attr
.ia_uid
, &attr
.ia_gid
);
670 if (!new_valid_dev(dev
))
673 smb_invalid_dir_cache(dir
);
674 error
= smb_proc_setattr_unix(dentry
, &attr
, MAJOR(dev
), MINOR(dev
));
676 error
= smb_instantiate(dentry
, 0, 0);
682 * dentry = existing file
683 * new_dentry = new file
686 smb_link(struct dentry
*dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
690 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
691 DENTRY_PATH(dentry
), DENTRY_PATH(new_dentry
));
692 smb_invalid_dir_cache(dir
);
693 error
= smb_proc_link(server_from_dentry(dentry
), dentry
, new_dentry
);
695 smb_renew_times(dentry
);
696 error
= smb_instantiate(new_dentry
, 0, 0);