nfs3proc.c 28 KB

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