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>
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
,
39 .readdir
= smb_readdir
,
44 struct inode_operations smb_dir_inode_operations
=
52 .getattr
= smb_getattr
,
53 .setattr
= smb_notify_change
,
56 struct inode_operations smb_dir_inode_operations_unix
=
64 .getattr
= smb_getattr
,
65 .setattr
= smb_notify_change
,
66 .symlink
= smb_symlink
,
67 .mknod
= smb_make_node
,
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
);
98 switch ((unsigned int) filp
->f_pos
) {
100 if (filldir(dirent
, ".", 1, 0, dir
->i_ino
, DT_DIR
) < 0)
105 if (filldir(dirent
, "..", 2, 1, parent_ino(dentry
), DT_DIR
) < 0)
111 * Make sure our inode is up-to-date.
113 result
= smb_revalidate_inode(dentry
);
118 page
= grab_cache_page(&dir
->i_data
, 0);
122 ctl
.cache
= cache
= kmap(page
);
123 ctl
.head
= cache
->head
;
125 if (!PageUptodate(page
) || !ctl
.head
.eof
) {
126 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
127 DENTRY_PATH(dentry
), PageUptodate(page
),ctl
.head
.eof
);
131 if (filp
->f_pos
== 2) {
132 if (jiffies
- ctl
.head
.time
>= SMB_MAX_AGE(server
))
136 * N.B. ncpfs checks mtime of dentry too here, we don't.
137 * 1. common smb servers do not update mtime on dir changes
138 * 2. it requires an extra smb request
139 * (revalidate has the same timeout as ctl.head.time)
141 * Instead smbfs invalidates its own cache on local changes
142 * and remote changes are not seen until timeout.
146 if (filp
->f_pos
> ctl
.head
.end
)
149 ctl
.fpos
= filp
->f_pos
+ (SMB_DIRCACHE_START
- 2);
150 ctl
.ofs
= ctl
.fpos
/ SMB_DIRCACHE_SIZE
;
151 ctl
.idx
= ctl
.fpos
% SMB_DIRCACHE_SIZE
;
155 ctl
.page
= find_lock_page(&dir
->i_data
, ctl
.ofs
);
158 ctl
.cache
= kmap(ctl
.page
);
159 if (!PageUptodate(ctl
.page
))
162 while (ctl
.idx
< SMB_DIRCACHE_SIZE
) {
166 dent
= smb_dget_fpos(ctl
.cache
->dentry
[ctl
.idx
],
167 dentry
, filp
->f_pos
);
171 res
= filldir(dirent
, dent
->d_name
.name
,
172 dent
->d_name
.len
, filp
->f_pos
,
173 dent
->d_inode
->i_ino
, DT_UNKNOWN
);
179 if (filp
->f_pos
> ctl
.head
.end
)
184 SetPageUptodate(ctl
.page
);
185 unlock_page(ctl
.page
);
186 page_cache_release(ctl
.page
);
195 unlock_page(ctl
.page
);
196 page_cache_release(ctl
.page
);
201 smb_invalidate_dircache_entries(dentry
);
202 ctl
.head
.time
= jiffies
;
206 ctl
.idx
= SMB_DIRCACHE_START
;
210 result
= server
->ops
->readdir(filp
, dirent
, filldir
, &ctl
);
212 goto invalid_cache
; /* retry */
213 ctl
.head
.end
= ctl
.fpos
- 1;
214 ctl
.head
.eof
= ctl
.valid
;
217 cache
->head
= ctl
.head
;
219 SetPageUptodate(page
);
221 page_cache_release(page
);
225 SetPageUptodate(ctl
.page
);
226 unlock_page(ctl
.page
);
227 page_cache_release(ctl
.page
);
235 smb_dir_open(struct inode
*dir
, struct file
*file
)
237 struct dentry
*dentry
= file
->f_dentry
;
238 struct smb_sb_info
*server
;
241 VERBOSE("(%s/%s)\n", dentry
->d_parent
->d_name
.name
,
242 file
->f_dentry
->d_name
.name
);
245 * Directory timestamps in the core protocol aren't updated
246 * when a file is added, so we give them a very short TTL.
249 server
= server_from_dentry(dentry
);
250 if (server
->opt
.protocol
< SMB_PROTOCOL_LANMAN2
) {
251 unsigned long age
= jiffies
- SMB_I(dir
)->oldmtime
;
253 smb_invalid_dir_cache(dir
);
257 * Note: in order to allow the smbmount process to open the
258 * mount point, we only revalidate if the connection is valid or
259 * if the process is trying to access something other than the root.
261 if (server
->state
== CONN_VALID
|| !IS_ROOT(dentry
))
262 error
= smb_revalidate_inode(dentry
);
268 * Dentry operations routines
270 static int smb_lookup_validate(struct dentry
*, int);
271 static int smb_hash_dentry(struct dentry
*, struct qstr
*);
272 static int smb_compare_dentry(struct dentry
*, struct qstr
*, struct qstr
*);
273 static int smb_delete_dentry(struct dentry
*);
275 static struct dentry_operations smbfs_dentry_operations
=
277 .d_revalidate
= smb_lookup_validate
,
278 .d_hash
= smb_hash_dentry
,
279 .d_compare
= smb_compare_dentry
,
280 .d_delete
= smb_delete_dentry
,
283 static struct dentry_operations smbfs_dentry_operations_case
=
285 .d_revalidate
= smb_lookup_validate
,
286 .d_delete
= smb_delete_dentry
,
291 * This is the callback when the dcache has a lookup hit.
294 smb_lookup_validate(struct dentry
* dentry
, int flags
)
296 struct smb_sb_info
*server
= server_from_dentry(dentry
);
297 struct inode
* inode
= dentry
->d_inode
;
298 unsigned long age
= jiffies
- dentry
->d_time
;
302 * The default validation is based on dentry age:
303 * we believe in dentries for a few seconds. (But each
304 * successful server lookup renews the timestamp.)
306 valid
= (age
<= SMB_MAX_AGE(server
));
307 #ifdef SMBFS_DEBUG_VERBOSE
309 VERBOSE("%s/%s not valid, age=%lu\n",
310 DENTRY_PATH(dentry
), age
);
315 if (is_bad_inode(inode
)) {
316 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry
));
319 valid
= (smb_revalidate_inode(dentry
) == 0);
323 * What should we do for negative dentries?
330 smb_hash_dentry(struct dentry
*dir
, struct qstr
*this)
335 hash
= init_name_hash();
336 for (i
=0; i
< this->len
; i
++)
337 hash
= partial_name_hash(tolower(this->name
[i
]), hash
);
338 this->hash
= end_name_hash(hash
);
344 smb_compare_dentry(struct dentry
*dir
, struct qstr
*a
, struct qstr
*b
)
348 if (a
->len
!= b
->len
)
350 for (i
=0; i
< a
->len
; i
++) {
351 if (tolower(a
->name
[i
]) != tolower(b
->name
[i
]))
360 * This is the callback from dput() when d_count is going to 0.
361 * We use this to unhash dentries with bad inodes.
364 smb_delete_dentry(struct dentry
* dentry
)
366 if (dentry
->d_inode
) {
367 if (is_bad_inode(dentry
->d_inode
)) {
368 PARANOIA("bad inode, unhashing %s/%s\n",
369 DENTRY_PATH(dentry
));
373 /* N.B. Unhash negative dentries? */
379 * Initialize a new dentry
382 smb_new_dentry(struct dentry
*dentry
)
384 struct smb_sb_info
*server
= server_from_dentry(dentry
);
386 if (server
->mnt
->flags
& SMB_MOUNT_CASE
)
387 dentry
->d_op
= &smbfs_dentry_operations_case
;
389 dentry
->d_op
= &smbfs_dentry_operations
;
390 dentry
->d_time
= jiffies
;
395 * Whenever a lookup succeeds, we know the parent directories
396 * are all valid, so we want to update the dentry timestamps.
397 * N.B. Move this to dcache?
400 smb_renew_times(struct dentry
* dentry
)
402 read_lock(&dparent_lock
);
404 dentry
->d_time
= jiffies
;
407 dentry
= dentry
->d_parent
;
409 read_unlock(&dparent_lock
);
412 static struct dentry
*
413 smb_lookup(struct inode
*dir
, struct dentry
*dentry
)
415 struct smb_fattr finfo
;
418 struct smb_sb_info
*server
;
420 error
= -ENAMETOOLONG
;
421 if (dentry
->d_name
.len
> SMB_MAXNAMELEN
)
425 error
= smb_proc_getattr(dentry
, &finfo
);
426 #ifdef SMBFS_PARANOIA
427 if (error
&& error
!= -ENOENT
)
428 PARANOIA("find %s/%s failed, error=%d\n",
429 DENTRY_PATH(dentry
), error
);
433 if (error
== -ENOENT
)
437 finfo
.f_ino
= iunique(dentry
->d_sb
, 2);
438 inode
= smb_iget(dir
->i_sb
, &finfo
);
441 server
= server_from_dentry(dentry
);
442 if (server
->mnt
->flags
& SMB_MOUNT_CASE
)
443 dentry
->d_op
= &smbfs_dentry_operations_case
;
445 dentry
->d_op
= &smbfs_dentry_operations
;
447 d_add(dentry
, inode
);
448 smb_renew_times(dentry
);
454 return ERR_PTR(error
);
458 * This code is common to all routines creating a new inode.
461 smb_instantiate(struct dentry
*dentry
, __u16 fileid
, int have_id
)
463 struct smb_sb_info
*server
= server_from_dentry(dentry
);
466 struct smb_fattr fattr
;
468 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry
), fileid
);
470 error
= smb_proc_getattr(dentry
, &fattr
);
474 smb_renew_times(dentry
);
475 fattr
.f_ino
= iunique(dentry
->d_sb
, 2);
476 inode
= smb_iget(dentry
->d_sb
, &fattr
);
481 struct smb_inode_info
*ei
= SMB_I(inode
);
483 ei
->access
= SMB_O_RDWR
;
484 ei
->open
= server
->generation
;
486 d_instantiate(dentry
, inode
);
494 PARANOIA("%s/%s failed, error=%d, closing %u\n",
495 DENTRY_PATH(dentry
), error
, fileid
);
496 smb_close_fileid(dentry
, fileid
);
501 /* N.B. How should the mode argument be used? */
503 smb_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
505 struct smb_sb_info
*server
= server_from_dentry(dentry
);
510 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry
), mode
);
513 smb_invalid_dir_cache(dir
);
514 error
= smb_proc_create(dentry
, 0, CURRENT_TIME
, &fileid
);
516 if (server
->opt
.capabilities
& SMB_CAP_UNIX
) {
517 /* Set attributes for new file */
518 attr
.ia_valid
= ATTR_MODE
;
520 error
= smb_proc_setattr_unix(dentry
, &attr
, 0, 0);
522 error
= smb_instantiate(dentry
, fileid
, 1);
524 PARANOIA("%s/%s failed, error=%d\n",
525 DENTRY_PATH(dentry
), error
);
531 /* N.B. How should the mode argument be used? */
533 smb_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
535 struct smb_sb_info
*server
= server_from_dentry(dentry
);
540 smb_invalid_dir_cache(dir
);
541 error
= smb_proc_mkdir(dentry
);
543 if (server
->opt
.capabilities
& SMB_CAP_UNIX
) {
544 /* Set attributes for new directory */
545 attr
.ia_valid
= ATTR_MODE
;
547 error
= smb_proc_setattr_unix(dentry
, &attr
, 0, 0);
549 error
= smb_instantiate(dentry
, 0, 0);
556 smb_rmdir(struct inode
*dir
, struct dentry
*dentry
)
558 struct inode
*inode
= dentry
->d_inode
;
562 * Close the directory if it's open.
568 * Check that nobody else is using the directory..
571 if (!d_unhashed(dentry
))
574 smb_invalid_dir_cache(dir
);
575 error
= smb_proc_rmdir(dentry
);
583 smb_unlink(struct inode
*dir
, struct dentry
*dentry
)
588 * Close the file if it's open.
591 smb_close(dentry
->d_inode
);
593 smb_invalid_dir_cache(dir
);
594 error
= smb_proc_unlink(dentry
);
596 smb_renew_times(dentry
);
602 smb_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
603 struct inode
*new_dir
, struct dentry
*new_dentry
)
608 * Close any open files, and check whether to delete the
609 * target before attempting the rename.
612 if (old_dentry
->d_inode
)
613 smb_close(old_dentry
->d_inode
);
614 if (new_dentry
->d_inode
) {
615 smb_close(new_dentry
->d_inode
);
616 error
= smb_proc_unlink(new_dentry
);
618 VERBOSE("unlink %s/%s, error=%d\n",
619 DENTRY_PATH(new_dentry
), error
);
623 d_delete(new_dentry
);
626 smb_invalid_dir_cache(old_dir
);
627 smb_invalid_dir_cache(new_dir
);
628 error
= smb_proc_mv(old_dentry
, new_dentry
);
630 smb_renew_times(old_dentry
);
631 smb_renew_times(new_dentry
);
639 * FIXME: samba servers won't let you create device nodes unless uid/gid
640 * matches the connection credentials (and we don't know which those are ...)
643 smb_make_node(struct inode
*dir
, struct dentry
*dentry
, int mode
, int dev
)
648 attr
.ia_valid
= ATTR_MODE
| ATTR_UID
| ATTR_GID
;
650 attr
.ia_uid
= current
->euid
;
651 attr
.ia_gid
= current
->egid
;
653 smb_invalid_dir_cache(dir
);
654 error
= smb_proc_setattr_unix(dentry
, &attr
, MAJOR(dev
), MINOR(dev
));
656 error
= smb_instantiate(dentry
, 0, 0);
662 * dentry = existing file
663 * new_dentry = new file
666 smb_link(struct dentry
*dentry
, struct inode
*dir
, struct dentry
*new_dentry
)
670 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
671 DENTRY_PATH(dentry
), DENTRY_PATH(new_dentry
));
672 smb_invalid_dir_cache(dir
);
673 error
= smb_proc_link(server_from_dentry(dentry
), dentry
, new_dentry
);
675 smb_renew_times(dentry
);
676 error
= smb_instantiate(new_dentry
, 0, 0);