nfs3proc.c 25 KB

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