nfs3proc.c 26 KB

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