nfs3proc.c 25 KB

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