12 #include <linux/kernel.h>
13 #include <linux/module.h>
18 #include <linux/ctype.h>
19 #include <linux/sched.h>
29 static int afs_lookup_filldir(
void *_cookie,
const char *
name,
int nlen,
40 static int afs_rename(
struct inode *old_dir,
struct dentry *old_dentry,
46 .readdir = afs_readdir,
56 .symlink = afs_symlink,
66 .d_revalidate = afs_d_revalidate,
67 .d_delete = afs_d_delete,
68 .d_release = afs_d_release,
72 #define AFS_DIR_HASHTBL_SIZE 128
73 #define AFS_DIR_DIRENT_SIZE 32
74 #define AFS_DIRENT_PER_BLOCK 64
95 #define AFS_DIR_MAGIC htons(1234)
131 static inline void afs_dir_check_page(
struct inode *dir,
struct page *
page)
144 printk(
"kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
145 __func__, dir->
i_ino, qty,
161 for (tmp = 0; tmp < qty; tmp++) {
163 printk(
"kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
164 __func__, dir->
i_ino, tmp, qty,
170 SetPageChecked(page);
174 SetPageChecked(page);
181 static inline void afs_dir_put_page(
struct page *page)
190 static struct page *afs_dir_get_page(
struct inode *dir,
unsigned long index,
199 if (!PageChecked(page))
200 afs_dir_check_page(dir, page);
207 afs_dir_put_page(page);
209 return ERR_PTR(-
EIO);
231 static int afs_dir_iterate_block(
unsigned *fpos,
242 _enter(
"%u,%x,%p,,",*fpos,blkoff,block);
244 curr = (*fpos - blkoff) /
sizeof(
union afs_dirent);
254 if (!(block->
pagehdr.bitmap[offset / 8] &
255 (1 << (offset % 8)))) {
256 _debug(
"ENT[%Zu.%u]: unused",
270 _debug(
"ENT[%Zu.%u]: %s %Zu \"%s\"",
272 (offset < curr ?
"skip" :
"fill"),
276 for (tmp = nlen; tmp > 15; tmp -=
sizeof(
union afs_dirent)) {
279 " %u travelled beyond end dir block"
282 offset, next, tmp, nlen);
285 if (!(block->
pagehdr.bitmap[next / 8] &
286 (1 << (next % 8)))) {
288 " %u unmarked extension (len %u/%Zu)",
290 offset, next, tmp, nlen);
294 _debug(
"ENT[%Zu.%u]: ext %u/%Zu",
305 ret = filldir(cookie,
310 filldir == afs_lookup_filldir ?
317 *fpos = blkoff + next *
sizeof(
union afs_dirent);
327 static int afs_dir_iterate(
struct inode *dir,
unsigned *fpos,
void *cookie,
333 unsigned blkoff,
limit;
349 while (*fpos < dir->i_size) {
353 page = afs_dir_get_page(dir, blkoff /
PAGE_SIZE, key);
367 ret = afs_dir_iterate_block(fpos, dblock, blkoff,
370 afs_dir_put_page(page);
376 }
while (*fpos < dir->i_size && blkoff < limit);
378 afs_dir_put_page(page);
390 static int afs_readdir(
struct file *file,
void *cookie,
filldir_t filldir)
401 ret = afs_dir_iterate(file->
f_path.dentry->d_inode, &fpos,
414 static int afs_lookup_filldir(
void *_cookie,
const char *
name,
int nlen,
419 _enter(
"{%s,%Zu},%s,%u,,%llu,%u",
420 cookie->
name, cookie->
nlen, name, nlen,
421 (
unsigned long long) ino, dtype);
427 if (cookie->
nlen != nlen ||
memcmp(cookie->
name, name, nlen) != 0) {
444 static int afs_do_lookup(
struct inode *dir,
struct dentry *
dentry,
454 as = dir->
i_sb->s_fs_info;
463 ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
466 _leave(
" = %d [iter]", ret);
472 _leave(
" = -ENOENT [not found]");
485 static struct inode *afs_try_auto_mntpt(
486 int ret,
struct dentry *dentry,
struct inode *dir,
struct key *key,
489 const char *devname = dentry->
d_name.name;
493 _enter(
"%d, %p{%s}, {%x:%u}, %p",
494 ret, dentry, devname, vnode->
fid.vid, vnode->
fid.vnode, key);
502 ret = PTR_ERR(inode);
506 *fid = AFS_FS_I(inode)->fid;
518 static struct dentry *afs_lookup(
struct inode *dir,
struct dentry *dentry,
527 vnode = AFS_FS_I(dir);
530 vnode->
fid.vid, vnode->
fid.vnode, dentry, dentry->
d_name.name);
535 _leave(
" = -ENAMETOOLONG");
546 _leave(
" = %ld [key]", PTR_ERR(key));
547 return ERR_CAST(key);
553 _leave(
" = %d [val]", ret);
557 ret = afs_do_lookup(dir, dentry, &fid, key);
559 inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid);
560 if (!IS_ERR(inode)) {
565 ret = PTR_ERR(inode);
569 _leave(
" = NULL [negative]");
572 _leave(
" = %d [do]", ret);
575 dentry->
d_fsdata = (
void *)(
unsigned long) vnode->
status.data_version;
581 _leave(
" = %ld", PTR_ERR(inode));
582 return ERR_CAST(inode);
586 d_add(dentry, inode);
587 _leave(
" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }",
591 dentry->
d_inode->i_generation);
601 static int afs_d_revalidate(
struct dentry *dentry,
unsigned int flags)
613 vnode = AFS_FS_I(dentry->
d_inode);
616 _enter(
"{v={%x:%u} n=%s fl=%lx},",
631 dir = AFS_FS_I(parent->
d_inode);
642 dir_version = (
void *) (
unsigned long) dir->
status.data_version;
643 if (dentry->
d_fsdata == dir_version)
649 ret = afs_do_lookup(&dir->
vfs_inode, dentry, &fid, key);
656 printk(
"kAFS: afs_d_revalidate: %s/%s has bad inode\n",
663 if (fid.
vnode != vnode->
fid.vnode) {
664 _debug(
"%s: dirent changed [%u != %u]",
674 _debug(
"%s: file deleted (uq %u -> %u I:%u)",
677 dentry->
d_inode->i_generation);
678 spin_lock(&vnode->
lock);
680 spin_unlock(&vnode->
lock);
693 _debug(
"failed to iterate dir %s: %d",
694 parent->
d_name.name, ret);
708 spin_lock(&dentry->
d_lock);
710 spin_unlock(&dentry->
d_lock);
719 _debug(
"dropping dentry %s/%s",
736 static int afs_d_delete(
const struct dentry *dentry)
759 static void afs_d_release(
struct dentry *dentry)
767 static int afs_mkdir(
struct inode *dir,
struct dentry *dentry,
umode_t mode)
778 dvnode = AFS_FS_I(dir);
780 _enter(
"{%x:%u},{%s},%ho",
781 dvnode->
fid.vid, dvnode->
fid.vnode, dentry->
d_name.name, mode);
803 ret = PTR_ERR(inode);
808 vnode = AFS_FS_I(inode);
809 spin_lock(&vnode->
lock);
811 spin_unlock(&vnode->
lock);
816 if (d_unhashed(dentry)) {
837 static int afs_rmdir(
struct inode *dir,
struct dentry *dentry)
843 dvnode = AFS_FS_I(dir);
846 dvnode->
fid.vid, dvnode->
fid.vnode, dentry->
d_name.name);
863 vnode = AFS_FS_I(dentry->
d_inode);
883 static int afs_unlink(
struct inode *dir,
struct dentry *dentry)
889 dvnode = AFS_FS_I(dir);
892 dvnode->
fid.vid, dvnode->
fid.vnode, dentry->
d_name.name);
905 vnode = AFS_FS_I(dentry->
d_inode);
927 vnode = AFS_FS_I(dentry->
d_inode);
929 _debug(
"AFS_VNODE_DELETED");
931 _debug(
"AFS_VNODE_CB_BROKEN");
951 static int afs_create(
struct inode *dir,
struct dentry *dentry,
umode_t mode,
963 dvnode = AFS_FS_I(dir);
965 _enter(
"{%x:%u},{%s},%ho,",
966 dvnode->
fid.vid, dvnode->
fid.vnode, dentry->
d_name.name, mode);
988 ret = PTR_ERR(inode);
993 vnode = AFS_FS_I(inode);
994 spin_lock(&vnode->
lock);
996 spin_unlock(&vnode->
lock);
1001 if (d_unhashed(dentry)) {
1022 static int afs_link(
struct dentry *
from,
struct inode *dir,
1023 struct dentry *dentry)
1029 vnode = AFS_FS_I(from->
d_inode);
1030 dvnode = AFS_FS_I(dir);
1032 _enter(
"{%x:%u},{%x:%u},{%s}",
1033 vnode->
fid.vid, vnode->
fid.vnode,
1034 dvnode->
fid.vid, dvnode->
fid.vnode,
1068 static int afs_symlink(
struct inode *dir,
struct dentry *dentry,
1069 const char *content)
1075 struct inode *
inode;
1079 dvnode = AFS_FS_I(dir);
1081 _enter(
"{%x:%u},{%s},%s",
1082 dvnode->
fid.vid, dvnode->
fid.vnode, dentry->
d_name.name,
1105 if (IS_ERR(inode)) {
1108 ret = PTR_ERR(inode);
1113 vnode = AFS_FS_I(inode);
1114 spin_lock(&vnode->
lock);
1116 spin_unlock(&vnode->
lock);
1121 if (d_unhashed(dentry)) {
1142 static int afs_rename(
struct inode *old_dir,
struct dentry *old_dentry,
1143 struct inode *new_dir,
struct dentry *new_dentry)
1145 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1149 vnode = AFS_FS_I(old_dentry->
d_inode);
1150 orig_dvnode = AFS_FS_I(old_dir);
1151 new_dvnode = AFS_FS_I(new_dir);
1153 _enter(
"{%x:%u},{%x:%u},{%x:%u},{%s}",
1154 orig_dvnode->
fid.vid, orig_dvnode->
fid.vnode,
1155 vnode->
fid.vid, vnode->
fid.vnode,
1156 new_dvnode->
fid.vid, new_dvnode->
fid.vnode,
1157 new_dentry->
d_name.name);
1171 new_dentry->
d_name.name);