nfs3proc.c 25 KB

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