nfs3proc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. /*
  2. * linux/fs/nfs/nfs3proc.c
  3. *
  4. * Client-side NFSv3 procedures stubs.
  5. *
  6. * Copyright (C) 1997, Olaf Kirch
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/errno.h>
  10. #include <linux/string.h>
  11. #include <linux/sunrpc/clnt.h>
  12. #include <linux/slab.h>
  13. #include <linux/nfs.h>
  14. #include <linux/nfs3.h>
  15. #include <linux/nfs_fs.h>
  16. #include <linux/nfs_page.h>
  17. #include <linux/lockd/bind.h>
  18. #include <linux/nfs_mount.h>
  19. #include <linux/freezer.h>
  20. #include <linux/xattr.h>
  21. #include "iostat.h"
  22. #include "internal.h"
  23. #include "nfs3_fs.h"
  24. #define NFSDBG_FACILITY NFSDBG_PROC
  25. /* A wrapper to handle the EJUKEBOX error messages */
  26. static int
  27. nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
  28. {
  29. int res;
  30. do {
  31. res = rpc_call_sync(clnt, msg, flags);
  32. if (res != -EJUKEBOX)
  33. break;
  34. freezable_schedule_timeout_killable_unsafe(NFS_JUKEBOX_RETRY_TIME);
  35. res = -ERESTARTSYS;
  36. } while (!fatal_signal_pending(current));
  37. return res;
  38. }
  39. #define rpc_call_sync(clnt, msg, flags) nfs3_rpc_wrapper(clnt, msg, flags)
  40. static int
  41. nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
  42. {
  43. if (task->tk_status != -EJUKEBOX)
  44. return 0;
  45. if (task->tk_status == -EJUKEBOX)
  46. nfs_inc_stats(inode, NFSIOS_DELAY);
  47. task->tk_status = 0;
  48. rpc_restart_call(task);
  49. rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
  50. return 1;
  51. }
  52. static int
  53. do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
  54. struct nfs_fsinfo *info)
  55. {
  56. struct rpc_message msg = {
  57. .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
  58. .rpc_argp = fhandle,
  59. .rpc_resp = info,
  60. };
  61. int status;
  62. dprintk("%s: call fsinfo\n", __func__);
  63. nfs_fattr_init(info->fattr);
  64. status = rpc_call_sync(client, &msg, 0);
  65. dprintk("%s: reply fsinfo: %d\n", __func__, status);
  66. if (status == 0 && !(info->fattr->valid & NFS_ATTR_FATTR)) {
  67. msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
  68. msg.rpc_resp = info->fattr;
  69. status = rpc_call_sync(client, &msg, 0);
  70. dprintk("%s: reply getattr: %d\n", __func__, status);
  71. }
  72. return status;
  73. }
  74. /*
  75. * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
  76. */
  77. static int
  78. nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  79. struct nfs_fsinfo *info)
  80. {
  81. int status;
  82. status = do_proc_get_root(server->client, fhandle, info);
  83. if (status && server->nfs_client->cl_rpcclient != server->client)
  84. status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
  85. return status;
  86. }
  87. /*
  88. * One function for each procedure in the NFS protocol.
  89. */
  90. static int
  91. nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  92. struct nfs_fattr *fattr, struct nfs4_label *label)
  93. {
  94. struct rpc_message msg = {
  95. .rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR],
  96. .rpc_argp = fhandle,
  97. .rpc_resp = fattr,
  98. };
  99. int status;
  100. dprintk("NFS call getattr\n");
  101. nfs_fattr_init(fattr);
  102. status = rpc_call_sync(server->client, &msg, 0);
  103. dprintk("NFS reply getattr: %d\n", status);
  104. return status;
  105. }
  106. static int
  107. nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  108. struct iattr *sattr)
  109. {
  110. struct inode *inode = d_inode(dentry);
  111. struct nfs3_sattrargs arg = {
  112. .fh = NFS_FH(inode),
  113. .sattr = sattr,
  114. };
  115. struct rpc_message msg = {
  116. .rpc_proc = &nfs3_procedures[NFS3PROC_SETATTR],
  117. .rpc_argp = &arg,
  118. .rpc_resp = fattr,
  119. };
  120. int status;
  121. dprintk("NFS call setattr\n");
  122. if (sattr->ia_valid & ATTR_FILE)
  123. msg.rpc_cred = nfs_file_cred(sattr->ia_file);
  124. nfs_fattr_init(fattr);
  125. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  126. if (status == 0)
  127. nfs_setattr_update_inode(inode, sattr, fattr);
  128. dprintk("NFS reply setattr: %d\n", status);
  129. return status;
  130. }
  131. static int
  132. nfs3_proc_lookup(struct inode *dir, const struct qstr *name,
  133. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  134. struct nfs4_label *label)
  135. {
  136. struct nfs3_diropargs arg = {
  137. .fh = NFS_FH(dir),
  138. .name = name->name,
  139. .len = name->len
  140. };
  141. struct nfs3_diropres res = {
  142. .fh = fhandle,
  143. .fattr = fattr
  144. };
  145. struct rpc_message msg = {
  146. .rpc_proc = &nfs3_procedures[NFS3PROC_LOOKUP],
  147. .rpc_argp = &arg,
  148. .rpc_resp = &res,
  149. };
  150. int status;
  151. dprintk("NFS call lookup %s\n", name->name);
  152. res.dir_attr = nfs_alloc_fattr();
  153. if (res.dir_attr == NULL)
  154. return -ENOMEM;
  155. nfs_fattr_init(fattr);
  156. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  157. nfs_refresh_inode(dir, res.dir_attr);
  158. if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
  159. msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
  160. msg.rpc_argp = fhandle;
  161. msg.rpc_resp = fattr;
  162. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  163. }
  164. nfs_free_fattr(res.dir_attr);
  165. dprintk("NFS reply lookup: %d\n", status);
  166. return status;
  167. }
  168. static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  169. {
  170. struct nfs3_accessargs arg = {
  171. .fh = NFS_FH(inode),
  172. .access = entry->mask,
  173. };
  174. struct nfs3_accessres res;
  175. struct rpc_message msg = {
  176. .rpc_proc = &nfs3_procedures[NFS3PROC_ACCESS],
  177. .rpc_argp = &arg,
  178. .rpc_resp = &res,
  179. .rpc_cred = entry->cred,
  180. };
  181. int status = -ENOMEM;
  182. dprintk("NFS call access\n");
  183. res.fattr = nfs_alloc_fattr();
  184. if (res.fattr == NULL)
  185. goto out;
  186. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  187. nfs_refresh_inode(inode, res.fattr);
  188. if (status == 0)
  189. nfs_access_set_mask(entry, res.access);
  190. nfs_free_fattr(res.fattr);
  191. out:
  192. dprintk("NFS reply access: %d\n", status);
  193. return status;
  194. }
  195. static int nfs3_proc_readlink(struct inode *inode, struct page *page,
  196. unsigned int pgbase, unsigned int pglen)
  197. {
  198. struct nfs_fattr *fattr;
  199. struct nfs3_readlinkargs args = {
  200. .fh = NFS_FH(inode),
  201. .pgbase = pgbase,
  202. .pglen = pglen,
  203. .pages = &page
  204. };
  205. struct rpc_message msg = {
  206. .rpc_proc = &nfs3_procedures[NFS3PROC_READLINK],
  207. .rpc_argp = &args,
  208. };
  209. int status = -ENOMEM;
  210. dprintk("NFS call readlink\n");
  211. fattr = nfs_alloc_fattr();
  212. if (fattr == NULL)
  213. goto out;
  214. msg.rpc_resp = fattr;
  215. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  216. nfs_refresh_inode(inode, fattr);
  217. nfs_free_fattr(fattr);
  218. out:
  219. dprintk("NFS reply readlink: %d\n", status);
  220. return status;
  221. }
  222. struct nfs3_createdata {
  223. struct rpc_message msg;
  224. union {
  225. struct nfs3_createargs create;
  226. struct nfs3_mkdirargs mkdir;
  227. struct nfs3_symlinkargs symlink;
  228. struct nfs3_mknodargs mknod;
  229. } arg;
  230. struct nfs3_diropres res;
  231. struct nfs_fh fh;
  232. struct nfs_fattr fattr;
  233. struct nfs_fattr dir_attr;
  234. };
  235. static struct nfs3_createdata *nfs3_alloc_createdata(void)
  236. {
  237. struct nfs3_createdata *data;
  238. data = kzalloc(sizeof(*data), GFP_KERNEL);
  239. if (data != NULL) {
  240. data->msg.rpc_argp = &data->arg;
  241. data->msg.rpc_resp = &data->res;
  242. data->res.fh = &data->fh;
  243. data->res.fattr = &data->fattr;
  244. data->res.dir_attr = &data->dir_attr;
  245. nfs_fattr_init(data->res.fattr);
  246. nfs_fattr_init(data->res.dir_attr);
  247. }
  248. return data;
  249. }
  250. static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
  251. {
  252. int status;
  253. status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
  254. nfs_post_op_update_inode(dir, data->res.dir_attr);
  255. if (status == 0)
  256. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
  257. return status;
  258. }
  259. static void nfs3_free_createdata(struct nfs3_createdata *data)
  260. {
  261. kfree(data);
  262. }
  263. /*
  264. * Create a regular file.
  265. */
  266. static int
  267. nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  268. int flags)
  269. {
  270. struct posix_acl *default_acl, *acl;
  271. struct nfs3_createdata *data;
  272. int status = -ENOMEM;
  273. dprintk("NFS call create %pd\n", dentry);
  274. data = nfs3_alloc_createdata();
  275. if (data == NULL)
  276. goto out;
  277. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
  278. data->arg.create.fh = NFS_FH(dir);
  279. data->arg.create.name = dentry->d_name.name;
  280. data->arg.create.len = dentry->d_name.len;
  281. data->arg.create.sattr = sattr;
  282. data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
  283. if (flags & O_EXCL) {
  284. data->arg.create.createmode = NFS3_CREATE_EXCLUSIVE;
  285. data->arg.create.verifier[0] = cpu_to_be32(jiffies);
  286. data->arg.create.verifier[1] = cpu_to_be32(current->pid);
  287. }
  288. status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
  289. if (status)
  290. goto out;
  291. for (;;) {
  292. status = nfs3_do_create(dir, dentry, data);
  293. if (status != -ENOTSUPP)
  294. break;
  295. /* If the server doesn't support the exclusive creation
  296. * semantics, try again with simple 'guarded' mode. */
  297. switch (data->arg.create.createmode) {
  298. case NFS3_CREATE_EXCLUSIVE:
  299. data->arg.create.createmode = NFS3_CREATE_GUARDED;
  300. break;
  301. case NFS3_CREATE_GUARDED:
  302. data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
  303. break;
  304. case NFS3_CREATE_UNCHECKED:
  305. goto out;
  306. }
  307. nfs_fattr_init(data->res.dir_attr);
  308. nfs_fattr_init(data->res.fattr);
  309. }
  310. if (status != 0)
  311. goto out_release_acls;
  312. /* When we created the file with exclusive semantics, make
  313. * sure we set the attributes afterwards. */
  314. if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
  315. dprintk("NFS call setattr (post-create)\n");
  316. if (!(sattr->ia_valid & ATTR_ATIME_SET))
  317. sattr->ia_valid |= ATTR_ATIME;
  318. if (!(sattr->ia_valid & ATTR_MTIME_SET))
  319. sattr->ia_valid |= ATTR_MTIME;
  320. /* Note: we could use a guarded setattr here, but I'm
  321. * not sure this buys us anything (and I'd have
  322. * to revamp the NFSv3 XDR code) */
  323. status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
  324. nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
  325. dprintk("NFS reply setattr (post-create): %d\n", status);
  326. if (status != 0)
  327. goto out_release_acls;
  328. }
  329. status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
  330. out_release_acls:
  331. posix_acl_release(acl);
  332. posix_acl_release(default_acl);
  333. out:
  334. nfs3_free_createdata(data);
  335. dprintk("NFS reply create: %d\n", status);
  336. return status;
  337. }
  338. static int
  339. nfs3_proc_remove(struct inode *dir, const struct qstr *name)
  340. {
  341. struct nfs_removeargs arg = {
  342. .fh = NFS_FH(dir),
  343. .name = *name,
  344. };
  345. struct nfs_removeres res;
  346. struct rpc_message msg = {
  347. .rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
  348. .rpc_argp = &arg,
  349. .rpc_resp = &res,
  350. };
  351. int status = -ENOMEM;
  352. dprintk("NFS call remove %s\n", name->name);
  353. res.dir_attr = nfs_alloc_fattr();
  354. if (res.dir_attr == NULL)
  355. goto out;
  356. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  357. nfs_post_op_update_inode(dir, res.dir_attr);
  358. nfs_free_fattr(res.dir_attr);
  359. out:
  360. dprintk("NFS reply remove: %d\n", status);
  361. return status;
  362. }
  363. static void
  364. nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  365. {
  366. msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
  367. }
  368. static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  369. {
  370. rpc_call_start(task);
  371. }
  372. static int
  373. nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  374. {
  375. struct nfs_removeres *res;
  376. if (nfs3_async_handle_jukebox(task, dir))
  377. return 0;
  378. res = task->tk_msg.rpc_resp;
  379. nfs_post_op_update_inode(dir, res->dir_attr);
  380. return 1;
  381. }
  382. static void
  383. nfs3_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  384. {
  385. msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
  386. }
  387. static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  388. {
  389. rpc_call_start(task);
  390. }
  391. static int
  392. nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  393. struct inode *new_dir)
  394. {
  395. struct nfs_renameres *res;
  396. if (nfs3_async_handle_jukebox(task, old_dir))
  397. return 0;
  398. res = task->tk_msg.rpc_resp;
  399. nfs_post_op_update_inode(old_dir, res->old_fattr);
  400. nfs_post_op_update_inode(new_dir, res->new_fattr);
  401. return 1;
  402. }
  403. static int
  404. nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  405. {
  406. struct nfs3_linkargs arg = {
  407. .fromfh = NFS_FH(inode),
  408. .tofh = NFS_FH(dir),
  409. .toname = name->name,
  410. .tolen = name->len
  411. };
  412. struct nfs3_linkres res;
  413. struct rpc_message msg = {
  414. .rpc_proc = &nfs3_procedures[NFS3PROC_LINK],
  415. .rpc_argp = &arg,
  416. .rpc_resp = &res,
  417. };
  418. int status = -ENOMEM;
  419. dprintk("NFS call link %s\n", name->name);
  420. res.fattr = nfs_alloc_fattr();
  421. res.dir_attr = nfs_alloc_fattr();
  422. if (res.fattr == NULL || res.dir_attr == NULL)
  423. goto out;
  424. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  425. nfs_post_op_update_inode(dir, res.dir_attr);
  426. nfs_post_op_update_inode(inode, res.fattr);
  427. out:
  428. nfs_free_fattr(res.dir_attr);
  429. nfs_free_fattr(res.fattr);
  430. dprintk("NFS reply link: %d\n", status);
  431. return status;
  432. }
  433. static int
  434. nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct page *page,
  435. unsigned int len, struct iattr *sattr)
  436. {
  437. struct nfs3_createdata *data;
  438. int status = -ENOMEM;
  439. if (len > NFS3_MAXPATHLEN)
  440. return -ENAMETOOLONG;
  441. dprintk("NFS call symlink %pd\n", dentry);
  442. data = nfs3_alloc_createdata();
  443. if (data == NULL)
  444. goto out;
  445. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
  446. data->arg.symlink.fromfh = NFS_FH(dir);
  447. data->arg.symlink.fromname = dentry->d_name.name;
  448. data->arg.symlink.fromlen = dentry->d_name.len;
  449. data->arg.symlink.pages = &page;
  450. data->arg.symlink.pathlen = len;
  451. data->arg.symlink.sattr = sattr;
  452. status = nfs3_do_create(dir, dentry, data);
  453. nfs3_free_createdata(data);
  454. out:
  455. dprintk("NFS reply symlink: %d\n", status);
  456. return status;
  457. }
  458. static int
  459. nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
  460. {
  461. struct posix_acl *default_acl, *acl;
  462. struct nfs3_createdata *data;
  463. int status = -ENOMEM;
  464. dprintk("NFS call mkdir %pd\n", dentry);
  465. data = nfs3_alloc_createdata();
  466. if (data == NULL)
  467. goto out;
  468. status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
  469. if (status)
  470. goto out;
  471. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
  472. data->arg.mkdir.fh = NFS_FH(dir);
  473. data->arg.mkdir.name = dentry->d_name.name;
  474. data->arg.mkdir.len = dentry->d_name.len;
  475. data->arg.mkdir.sattr = sattr;
  476. status = nfs3_do_create(dir, dentry, data);
  477. if (status != 0)
  478. goto out_release_acls;
  479. status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
  480. out_release_acls:
  481. posix_acl_release(acl);
  482. posix_acl_release(default_acl);
  483. out:
  484. nfs3_free_createdata(data);
  485. dprintk("NFS reply mkdir: %d\n", status);
  486. return status;
  487. }
  488. static int
  489. nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
  490. {
  491. struct nfs_fattr *dir_attr;
  492. struct nfs3_diropargs arg = {
  493. .fh = NFS_FH(dir),
  494. .name = name->name,
  495. .len = name->len
  496. };
  497. struct rpc_message msg = {
  498. .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
  499. .rpc_argp = &arg,
  500. };
  501. int status = -ENOMEM;
  502. dprintk("NFS call rmdir %s\n", name->name);
  503. dir_attr = nfs_alloc_fattr();
  504. if (dir_attr == NULL)
  505. goto out;
  506. msg.rpc_resp = dir_attr;
  507. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  508. nfs_post_op_update_inode(dir, dir_attr);
  509. nfs_free_fattr(dir_attr);
  510. out:
  511. dprintk("NFS reply rmdir: %d\n", status);
  512. return status;
  513. }
  514. /*
  515. * The READDIR implementation is somewhat hackish - we pass the user buffer
  516. * to the encode function, which installs it in the receive iovec.
  517. * The decode function itself doesn't perform any decoding, it just makes
  518. * sure the reply is syntactically correct.
  519. *
  520. * Also note that this implementation handles both plain readdir and
  521. * readdirplus.
  522. */
  523. static int
  524. nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  525. u64 cookie, struct page **pages, unsigned int count, bool plus)
  526. {
  527. struct inode *dir = d_inode(dentry);
  528. __be32 *verf = NFS_I(dir)->cookieverf;
  529. struct nfs3_readdirargs arg = {
  530. .fh = NFS_FH(dir),
  531. .cookie = cookie,
  532. .verf = {verf[0], verf[1]},
  533. .plus = plus,
  534. .count = count,
  535. .pages = pages
  536. };
  537. struct nfs3_readdirres res = {
  538. .verf = verf,
  539. .plus = plus
  540. };
  541. struct rpc_message msg = {
  542. .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
  543. .rpc_argp = &arg,
  544. .rpc_resp = &res,
  545. .rpc_cred = cred
  546. };
  547. int status = -ENOMEM;
  548. if (plus)
  549. msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
  550. dprintk("NFS call readdir%s %d\n",
  551. plus? "plus" : "", (unsigned int) cookie);
  552. res.dir_attr = nfs_alloc_fattr();
  553. if (res.dir_attr == NULL)
  554. goto out;
  555. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  556. nfs_invalidate_atime(dir);
  557. nfs_refresh_inode(dir, res.dir_attr);
  558. nfs_free_fattr(res.dir_attr);
  559. out:
  560. dprintk("NFS reply readdir%s: %d\n",
  561. plus? "plus" : "", status);
  562. return status;
  563. }
  564. static int
  565. nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  566. dev_t rdev)
  567. {
  568. struct posix_acl *default_acl, *acl;
  569. struct nfs3_createdata *data;
  570. int status = -ENOMEM;
  571. dprintk("NFS call mknod %pd %u:%u\n", dentry,
  572. MAJOR(rdev), MINOR(rdev));
  573. data = nfs3_alloc_createdata();
  574. if (data == NULL)
  575. goto out;
  576. status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
  577. if (status)
  578. goto out;
  579. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
  580. data->arg.mknod.fh = NFS_FH(dir);
  581. data->arg.mknod.name = dentry->d_name.name;
  582. data->arg.mknod.len = dentry->d_name.len;
  583. data->arg.mknod.sattr = sattr;
  584. data->arg.mknod.rdev = rdev;
  585. switch (sattr->ia_mode & S_IFMT) {
  586. case S_IFBLK:
  587. data->arg.mknod.type = NF3BLK;
  588. break;
  589. case S_IFCHR:
  590. data->arg.mknod.type = NF3CHR;
  591. break;
  592. case S_IFIFO:
  593. data->arg.mknod.type = NF3FIFO;
  594. break;
  595. case S_IFSOCK:
  596. data->arg.mknod.type = NF3SOCK;
  597. break;
  598. default:
  599. status = -EINVAL;
  600. goto out;
  601. }
  602. status = nfs3_do_create(dir, dentry, data);
  603. if (status != 0)
  604. goto out_release_acls;
  605. status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
  606. out_release_acls:
  607. posix_acl_release(acl);
  608. posix_acl_release(default_acl);
  609. out:
  610. nfs3_free_createdata(data);
  611. dprintk("NFS reply mknod: %d\n", status);
  612. return status;
  613. }
  614. static int
  615. nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  616. struct nfs_fsstat *stat)
  617. {
  618. struct rpc_message msg = {
  619. .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
  620. .rpc_argp = fhandle,
  621. .rpc_resp = stat,
  622. };
  623. int status;
  624. dprintk("NFS call fsstat\n");
  625. nfs_fattr_init(stat->fattr);
  626. status = rpc_call_sync(server->client, &msg, 0);
  627. dprintk("NFS reply fsstat: %d\n", status);
  628. return status;
  629. }
  630. static int
  631. do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
  632. struct nfs_fsinfo *info)
  633. {
  634. struct rpc_message msg = {
  635. .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
  636. .rpc_argp = fhandle,
  637. .rpc_resp = info,
  638. };
  639. int status;
  640. dprintk("NFS call fsinfo\n");
  641. nfs_fattr_init(info->fattr);
  642. status = rpc_call_sync(client, &msg, 0);
  643. dprintk("NFS reply fsinfo: %d\n", status);
  644. return status;
  645. }
  646. /*
  647. * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
  648. * nfs_create_server
  649. */
  650. static int
  651. nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  652. struct nfs_fsinfo *info)
  653. {
  654. int status;
  655. status = do_proc_fsinfo(server->client, fhandle, info);
  656. if (status && server->nfs_client->cl_rpcclient != server->client)
  657. status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
  658. return status;
  659. }
  660. static int
  661. nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  662. struct nfs_pathconf *info)
  663. {
  664. struct rpc_message msg = {
  665. .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
  666. .rpc_argp = fhandle,
  667. .rpc_resp = info,
  668. };
  669. int status;
  670. dprintk("NFS call pathconf\n");
  671. nfs_fattr_init(info->fattr);
  672. status = rpc_call_sync(server->client, &msg, 0);
  673. dprintk("NFS reply pathconf: %d\n", status);
  674. return status;
  675. }
  676. static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  677. {
  678. struct inode *inode = hdr->inode;
  679. if (hdr->pgio_done_cb != NULL)
  680. return hdr->pgio_done_cb(task, hdr);
  681. if (nfs3_async_handle_jukebox(task, inode))
  682. return -EAGAIN;
  683. nfs_invalidate_atime(inode);
  684. nfs_refresh_inode(inode, &hdr->fattr);
  685. return 0;
  686. }
  687. static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
  688. struct rpc_message *msg)
  689. {
  690. msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
  691. }
  692. static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
  693. struct nfs_pgio_header *hdr)
  694. {
  695. rpc_call_start(task);
  696. return 0;
  697. }
  698. static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  699. {
  700. struct inode *inode = hdr->inode;
  701. if (hdr->pgio_done_cb != NULL)
  702. return hdr->pgio_done_cb(task, hdr);
  703. if (nfs3_async_handle_jukebox(task, inode))
  704. return -EAGAIN;
  705. if (task->tk_status >= 0)
  706. nfs_writeback_update_inode(hdr);
  707. return 0;
  708. }
  709. static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
  710. struct rpc_message *msg)
  711. {
  712. msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
  713. }
  714. static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  715. {
  716. rpc_call_start(task);
  717. }
  718. static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  719. {
  720. if (data->commit_done_cb != NULL)
  721. return data->commit_done_cb(task, data);
  722. if (nfs3_async_handle_jukebox(task, data->inode))
  723. return -EAGAIN;
  724. nfs_refresh_inode(data->inode, data->res.fattr);
  725. return 0;
  726. }
  727. static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  728. {
  729. msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
  730. }
  731. static void nfs3_nlm_alloc_call(void *data)
  732. {
  733. struct nfs_lock_context *l_ctx = data;
  734. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
  735. get_nfs_open_context(l_ctx->open_context);
  736. nfs_get_lock_context(l_ctx->open_context);
  737. }
  738. }
  739. static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
  740. {
  741. struct nfs_lock_context *l_ctx = data;
  742. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
  743. return nfs_async_iocounter_wait(task, l_ctx);
  744. return false;
  745. }
  746. static void nfs3_nlm_release_call(void *data)
  747. {
  748. struct nfs_lock_context *l_ctx = data;
  749. struct nfs_open_context *ctx;
  750. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
  751. ctx = l_ctx->open_context;
  752. nfs_put_lock_context(l_ctx);
  753. put_nfs_open_context(ctx);
  754. }
  755. }
  756. const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
  757. .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
  758. .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
  759. .nlmclnt_release_call = nfs3_nlm_release_call,
  760. };
  761. static int
  762. nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
  763. {
  764. struct inode *inode = file_inode(filp);
  765. struct nfs_lock_context *l_ctx = NULL;
  766. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  767. int status;
  768. if (fl->fl_flags & FL_CLOSE) {
  769. l_ctx = nfs_get_lock_context(ctx);
  770. if (IS_ERR(l_ctx))
  771. l_ctx = NULL;
  772. else
  773. set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
  774. }
  775. status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
  776. if (l_ctx)
  777. nfs_put_lock_context(l_ctx);
  778. return status;
  779. }
  780. static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
  781. {
  782. return 0;
  783. }
  784. static int nfs3_return_delegation(struct inode *inode)
  785. {
  786. nfs_wb_all(inode);
  787. return 0;
  788. }
  789. static const struct inode_operations nfs3_dir_inode_operations = {
  790. .create = nfs_create,
  791. .lookup = nfs_lookup,
  792. .link = nfs_link,
  793. .unlink = nfs_unlink,
  794. .symlink = nfs_symlink,
  795. .mkdir = nfs_mkdir,
  796. .rmdir = nfs_rmdir,
  797. .mknod = nfs_mknod,
  798. .rename = nfs_rename,
  799. .permission = nfs_permission,
  800. .getattr = nfs_getattr,
  801. .setattr = nfs_setattr,
  802. #ifdef CONFIG_NFS_V3_ACL
  803. .listxattr = nfs3_listxattr,
  804. .get_acl = nfs3_get_acl,
  805. .set_acl = nfs3_set_acl,
  806. #endif
  807. };
  808. static const struct inode_operations nfs3_file_inode_operations = {
  809. .permission = nfs_permission,
  810. .getattr = nfs_getattr,
  811. .setattr = nfs_setattr,
  812. #ifdef CONFIG_NFS_V3_ACL
  813. .listxattr = nfs3_listxattr,
  814. .get_acl = nfs3_get_acl,
  815. .set_acl = nfs3_set_acl,
  816. #endif
  817. };
  818. const struct nfs_rpc_ops nfs_v3_clientops = {
  819. .version = 3, /* protocol version */
  820. .dentry_ops = &nfs_dentry_operations,
  821. .dir_inode_ops = &nfs3_dir_inode_operations,
  822. .file_inode_ops = &nfs3_file_inode_operations,
  823. .file_ops = &nfs_file_operations,
  824. .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
  825. .getroot = nfs3_proc_get_root,
  826. .submount = nfs_submount,
  827. .try_mount = nfs_try_mount,
  828. .getattr = nfs3_proc_getattr,
  829. .setattr = nfs3_proc_setattr,
  830. .lookup = nfs3_proc_lookup,
  831. .access = nfs3_proc_access,
  832. .readlink = nfs3_proc_readlink,
  833. .create = nfs3_proc_create,
  834. .remove = nfs3_proc_remove,
  835. .unlink_setup = nfs3_proc_unlink_setup,
  836. .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
  837. .unlink_done = nfs3_proc_unlink_done,
  838. .rename_setup = nfs3_proc_rename_setup,
  839. .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
  840. .rename_done = nfs3_proc_rename_done,
  841. .link = nfs3_proc_link,
  842. .symlink = nfs3_proc_symlink,
  843. .mkdir = nfs3_proc_mkdir,
  844. .rmdir = nfs3_proc_rmdir,
  845. .readdir = nfs3_proc_readdir,
  846. .mknod = nfs3_proc_mknod,
  847. .statfs = nfs3_proc_statfs,
  848. .fsinfo = nfs3_proc_fsinfo,
  849. .pathconf = nfs3_proc_pathconf,
  850. .decode_dirent = nfs3_decode_dirent,
  851. .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
  852. .read_setup = nfs3_proc_read_setup,
  853. .read_done = nfs3_read_done,
  854. .write_setup = nfs3_proc_write_setup,
  855. .write_done = nfs3_write_done,
  856. .commit_setup = nfs3_proc_commit_setup,
  857. .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
  858. .commit_done = nfs3_commit_done,
  859. .lock = nfs3_proc_lock,
  860. .clear_acl_cache = forget_all_cached_acls,
  861. .close_context = nfs_close_context,
  862. .have_delegation = nfs3_have_delegation,
  863. .return_delegation = nfs3_return_delegation,
  864. .alloc_client = nfs_alloc_client,
  865. .init_client = nfs_init_client,
  866. .free_client = nfs_free_client,
  867. .create_server = nfs3_create_server,
  868. .clone_server = nfs3_clone_server,
  869. };