nfs3proc.c 26 KB

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