4 * Copyright (C) 1992, 1993, 1994 Rick Sladkey
6 * OS-independent nfs remote procedure call functions
8 * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
9 * so at last we can have decent(ish) throughput off a
12 * Coding optimized and cleaned up by Florian La Roche.
13 * Note: Error returns are optimized for NFS_OK, which isn't translated via
14 * nfs_stat_to_errno(), but happens to be already the right return code.
16 * Also, the code currently doesn't check the size of the packet, when
17 * it decodes the packet.
19 * Feel free to fix it and mail me the diffs if it worries you.
21 * Completely rewritten to support the new RPC call interface;
22 * rewrote and moved the entire XDR stuff to xdr.c
23 * --Olaf Kirch June 1996
25 * The code below initializes all auto variables explicitly, otherwise
26 * it will fail to work as a module (gcc generates a memset call for an
30 #include <linux/types.h>
31 #include <linux/param.h>
32 #include <linux/malloc.h>
33 #include <linux/sched.h>
35 #include <linux/utsname.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
39 #include <linux/pagemap.h>
40 #include <linux/sunrpc/clnt.h>
41 #include <linux/nfs.h>
42 #include <linux/nfs2.h>
43 #include <linux/nfs_fs.h>
45 #define NFSDBG_FACILITY NFSDBG_PROC
48 * Bare-bones access to getattr: this is for nfs_read_super.
51 nfs_proc_get_root(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
52 struct nfs_fattr
*fattr
)
56 dprintk("NFS call getroot\n");
58 status
= rpc_call(server
->client
, NFSPROC_GETATTR
, fhandle
, fattr
, 0);
59 dprintk("NFS reply getroot\n");
64 * One function for each procedure in the NFS protocol.
67 nfs_proc_getattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
)
71 dprintk("NFS call getattr\n");
73 status
= rpc_call(NFS_CLIENT(dentry
->d_inode
), NFSPROC_GETATTR
,
74 NFS_FH(dentry
), fattr
, 0);
75 dprintk("NFS reply getattr\n");
80 nfs_proc_setattr(struct dentry
*dentry
, struct nfs_fattr
*fattr
,
83 struct nfs_sattrargs arg
= { NFS_FH(dentry
), sattr
};
86 dprintk("NFS call setattr\n");
88 status
= rpc_call(NFS_CLIENT(dentry
->d_inode
), NFSPROC_SETATTR
, &arg
, fattr
, 0);
89 dprintk("NFS reply setattr\n");
94 nfs_proc_lookup(struct dentry
*dir
, struct qstr
*name
,
95 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
97 struct nfs_diropargs arg
= { NFS_FH(dir
), name
->name
, name
->len
};
98 struct nfs_diropok res
= { fhandle
, fattr
};
101 dprintk("NFS call lookup %s\n", name
->name
);
103 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_LOOKUP
, &arg
, &res
, 0);
104 dprintk("NFS reply lookup: %d\n", status
);
109 nfs_proc_readlink(struct dentry
*dentry
, void *buffer
, unsigned int bufsiz
)
111 struct nfs_readlinkargs args
= { NFS_FH(dentry
), buffer
, bufsiz
};
112 struct nfs_readlinkres res
= { buffer
, bufsiz
};
115 dprintk("NFS call readlink\n");
116 status
= rpc_call(NFS_CLIENT(dentry
->d_inode
), NFSPROC_READLINK
,
118 dprintk("NFS reply readlink: %d\n", status
);
123 nfs_proc_read(struct file
*file
, struct nfs_fattr
*fattr
, int flags
,
124 loff_t offset
, unsigned int count
, void *buffer
, int *eofp
)
126 struct dentry
*dentry
= file
->f_dentry
;
127 struct rpc_cred
*cred
= nfs_file_cred(file
);
128 struct nfs_readargs arg
= { NFS_FH(dentry
), offset
, count
, 1,
129 {{ buffer
, count
}, {0,0}, {0,0}, {0,0},
130 {0,0}, {0,0}, {0,0}, {0,0}} };
131 struct nfs_readres res
= { fattr
, count
, 0};
132 struct rpc_message msg
= { NFSPROC_READ
, &arg
, &res
, cred
};
135 dprintk("NFS call read %d @ %Ld\n", count
, (long long)offset
);
137 status
= rpc_call_sync(NFS_CLIENT(dentry
->d_inode
), &msg
, flags
);
139 dprintk("NFS reply read: %d\n", status
);
145 nfs_proc_write(struct file
*file
, struct nfs_fattr
*fattr
, int how
,
146 loff_t offset
, unsigned int count
,
147 void *buffer
, struct nfs_writeverf
*verf
)
149 struct dentry
*dentry
= file
->f_dentry
;
150 struct rpc_cred
*cred
= nfs_file_cred(file
);
151 struct nfs_writeargs arg
= {NFS_FH(dentry
), offset
, count
,
153 {{buffer
, count
}, {0,0}, {0,0}, {0,0},
154 {0,0}, {0,0}, {0,0}, {0,0}}};
155 struct nfs_writeres res
= {fattr
, verf
, count
};
156 struct rpc_message msg
= { NFSPROC_WRITE
, &arg
, &res
, cred
};
157 int status
, flags
= 0;
159 dprintk("NFS call write %d @ %Ld\n", count
, (long long)offset
);
161 if (how
& NFS_RW_SWAP
)
162 flags
|= NFS_RPC_SWAPFLAGS
;
163 status
= rpc_call_sync(NFS_CLIENT(dentry
->d_inode
), &msg
, flags
);
165 dprintk("NFS reply write: %d\n", status
);
166 verf
->committed
= NFS_FILE_SYNC
; /* NFSv2 always syncs data */
167 return status
< 0? status
: count
;
171 nfs_proc_create(struct dentry
*dir
, struct qstr
*name
, struct iattr
*sattr
,
172 int flags
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
174 struct nfs_createargs arg
= { NFS_FH(dir
), name
->name
,
176 struct nfs_diropok res
= { fhandle
, fattr
};
180 dprintk("NFS call create %s\n", name
->name
);
181 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_CREATE
, &arg
, &res
, 0);
182 dprintk("NFS reply create: %d\n", status
);
187 * In NFSv2, mknod is grafted onto the create call.
190 nfs_proc_mknod(struct dentry
*dir
, struct qstr
*name
, struct iattr
*sattr
,
191 dev_t rdev
, struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
193 struct nfs_createargs arg
= { NFS_FH(dir
), name
->name
,
195 struct nfs_diropok res
= { fhandle
, fattr
};
198 dprintk("NFS call mknod %s\n", name
->name
);
200 mode
= sattr
->ia_mode
;
201 if (S_ISFIFO(mode
)) {
202 sattr
->ia_mode
= (mode
& ~S_IFMT
) | S_IFCHR
;
203 sattr
->ia_valid
&= ~ATTR_SIZE
;
204 } else if (S_ISCHR(mode
) || S_ISBLK(mode
)) {
205 sattr
->ia_valid
|= ATTR_SIZE
;
206 sattr
->ia_size
= rdev
; /* get out your barf bag */
210 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_CREATE
, &arg
, &res
, 0);
212 if (status
== -EINVAL
&& S_ISFIFO(mode
)) {
213 sattr
->ia_mode
= mode
;
215 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_CREATE
, &arg
, &res
, 0);
217 dprintk("NFS reply mknod: %d\n", status
);
222 nfs_proc_remove(struct dentry
*dir
, struct qstr
*name
)
224 struct nfs_diropargs arg
= { NFS_FH(dir
), name
->name
, name
->len
};
225 struct rpc_message msg
= { NFSPROC_REMOVE
, &arg
, NULL
, NULL
};
228 dprintk("NFS call remove %s\n", name
->name
);
229 status
= rpc_call_sync(NFS_CLIENT(dir
->d_inode
), &msg
, 0);
231 dprintk("NFS reply remove: %d\n", status
);
236 nfs_proc_unlink_setup(struct rpc_message
*msg
, struct dentry
*dir
, struct qstr
*name
)
238 struct nfs_diropargs
*arg
;
240 arg
= (struct nfs_diropargs
*)kmalloc(sizeof(*arg
), GFP_KERNEL
);
243 arg
->fh
= NFS_FH(dir
);
244 arg
->name
= name
->name
;
245 arg
->len
= name
->len
;
246 msg
->rpc_proc
= NFSPROC_REMOVE
;
252 nfs_proc_unlink_done(struct dentry
*dir
, struct rpc_message
*msg
)
254 NFS_CACHEINV(dir
->d_inode
);
255 kfree(msg
->rpc_argp
);
259 nfs_proc_rename(struct dentry
*old_dir
, struct qstr
*old_name
,
260 struct dentry
*new_dir
, struct qstr
*new_name
)
262 struct nfs_renameargs arg
= { NFS_FH(old_dir
), old_name
->name
,
264 NFS_FH(new_dir
), new_name
->name
,
268 dprintk("NFS call rename %s -> %s\n", old_name
->name
, new_name
->name
);
269 status
= rpc_call(NFS_CLIENT(old_dir
->d_inode
), NFSPROC_RENAME
, &arg
, NULL
, 0);
270 dprintk("NFS reply rename: %d\n", status
);
275 nfs_proc_link(struct dentry
*dentry
, struct dentry
*dir
, struct qstr
*name
)
277 struct nfs_linkargs arg
= { NFS_FH(dentry
), NFS_FH(dir
),
278 name
->name
, name
->len
};
281 dprintk("NFS call link %s\n", name
->name
);
282 status
= rpc_call(NFS_CLIENT(dentry
->d_inode
), NFSPROC_LINK
, &arg
, NULL
, 0);
283 dprintk("NFS reply link: %d\n", status
);
288 nfs_proc_symlink(struct dentry
*dir
, struct qstr
*name
, struct qstr
*path
,
289 struct iattr
*sattr
, struct nfs_fh
*fhandle
,
290 struct nfs_fattr
*fattr
)
292 struct nfs_symlinkargs arg
= { NFS_FH(dir
), name
->name
, name
->len
,
293 path
->name
, path
->len
, sattr
};
296 dprintk("NFS call symlink %s -> %s\n", name
->name
, path
->name
);
298 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_SYMLINK
, &arg
, NULL
, 0);
299 dprintk("NFS reply symlink: %d\n", status
);
304 nfs_proc_mkdir(struct dentry
*dir
, struct qstr
*name
, struct iattr
*sattr
,
305 struct nfs_fh
*fhandle
, struct nfs_fattr
*fattr
)
307 struct nfs_createargs arg
= { NFS_FH(dir
), name
->name
, name
->len
,
309 struct nfs_diropok res
= { fhandle
, fattr
};
312 dprintk("NFS call mkdir %s\n", name
->name
);
314 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_MKDIR
, &arg
, &res
, 0);
315 dprintk("NFS reply mkdir: %d\n", status
);
320 nfs_proc_rmdir(struct dentry
*dir
, struct qstr
*name
)
322 struct nfs_diropargs arg
= { NFS_FH(dir
), name
->name
, name
->len
};
325 dprintk("NFS call rmdir %s\n", name
->name
);
326 status
= rpc_call(NFS_CLIENT(dir
->d_inode
), NFSPROC_RMDIR
, &arg
, NULL
, 0);
327 dprintk("NFS reply rmdir: %d\n", status
);
332 * The READDIR implementation is somewhat hackish - we pass a temporary
333 * buffer to the encode function, which installs it in the receive
334 * the receive iovec. The decode function just parses the reply to make
335 * sure it is syntactically correct; the entries itself are decoded
336 * from nfs_readdir by calling the decode_entry function directly.
339 nfs_proc_readdir(struct file
*file
, __u64 cookie
, void *entry
,
340 unsigned int size
, int plus
)
342 struct dentry
*dir
= file
->f_dentry
;
343 struct rpc_cred
*cred
= nfs_file_cred(file
);
344 struct nfs_readdirargs arg
;
345 struct nfs_readdirres res
;
346 struct rpc_message msg
= { NFSPROC_READDIR
, &arg
, &res
, cred
};
349 arg
.fh
= NFS_FH(dir
);
356 dprintk("NFS call readdir %d\n", (unsigned int)cookie
);
357 status
= rpc_call_sync(NFS_CLIENT(dir
->d_inode
), &msg
, 0);
359 dprintk("NFS reply readdir: %d\n", status
);
364 nfs_proc_statfs(struct nfs_server
*server
, struct nfs_fh
*fhandle
,
365 struct nfs_fsinfo
*info
)
369 dprintk("NFS call statfs\n");
370 memset((char *)info
, 0, sizeof(*info
));
371 status
= rpc_call(server
->client
, NFSPROC_STATFS
, fhandle
, info
, 0);
372 dprintk("NFS reply statfs: %d\n", status
);
376 extern u32
* nfs_decode_dirent(u32
*, struct nfs_entry
*, int);
378 struct nfs_rpc_ops nfs_v2_clientops
= {
379 2, /* protocol version */
391 nfs_proc_unlink_setup
,
392 nfs_proc_unlink_done
,