nfs3proc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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,
  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 folio *folio,
  478. unsigned int len, struct iattr *sattr)
  479. {
  480. struct page *page = &folio->page;
  481. struct nfs3_createdata *data;
  482. struct dentry *d_alias;
  483. int status = -ENOMEM;
  484. if (len > NFS3_MAXPATHLEN)
  485. return -ENAMETOOLONG;
  486. dprintk("NFS call symlink %pd\n", dentry);
  487. data = nfs3_alloc_createdata();
  488. if (data == NULL)
  489. goto out;
  490. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
  491. data->arg.symlink.fromfh = NFS_FH(dir);
  492. data->arg.symlink.fromname = dentry->d_name.name;
  493. data->arg.symlink.fromlen = dentry->d_name.len;
  494. data->arg.symlink.pages = &page;
  495. data->arg.symlink.pathlen = len;
  496. data->arg.symlink.sattr = sattr;
  497. d_alias = nfs3_do_create(dir, dentry, data);
  498. status = PTR_ERR_OR_ZERO(d_alias);
  499. if (status == 0)
  500. dput(d_alias);
  501. nfs3_free_createdata(data);
  502. out:
  503. dprintk("NFS reply symlink: %d\n", status);
  504. return status;
  505. }
  506. static int
  507. nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
  508. {
  509. struct posix_acl *default_acl, *acl;
  510. struct nfs3_createdata *data;
  511. struct dentry *d_alias;
  512. int status = -ENOMEM;
  513. dprintk("NFS call mkdir %pd\n", dentry);
  514. data = nfs3_alloc_createdata();
  515. if (data == NULL)
  516. goto out;
  517. status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
  518. if (status)
  519. goto out;
  520. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
  521. data->arg.mkdir.fh = NFS_FH(dir);
  522. data->arg.mkdir.name = dentry->d_name.name;
  523. data->arg.mkdir.len = dentry->d_name.len;
  524. data->arg.mkdir.sattr = sattr;
  525. d_alias = nfs3_do_create(dir, dentry, data);
  526. status = PTR_ERR_OR_ZERO(d_alias);
  527. if (status != 0)
  528. goto out_release_acls;
  529. if (d_alias)
  530. dentry = d_alias;
  531. status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
  532. dput(d_alias);
  533. out_release_acls:
  534. posix_acl_release(acl);
  535. posix_acl_release(default_acl);
  536. out:
  537. nfs3_free_createdata(data);
  538. dprintk("NFS reply mkdir: %d\n", status);
  539. return status;
  540. }
  541. static int
  542. nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
  543. {
  544. struct nfs_fattr *dir_attr;
  545. struct nfs3_diropargs arg = {
  546. .fh = NFS_FH(dir),
  547. .name = name->name,
  548. .len = name->len
  549. };
  550. struct rpc_message msg = {
  551. .rpc_proc = &nfs3_procedures[NFS3PROC_RMDIR],
  552. .rpc_argp = &arg,
  553. };
  554. int status = -ENOMEM;
  555. dprintk("NFS call rmdir %s\n", name->name);
  556. dir_attr = nfs_alloc_fattr();
  557. if (dir_attr == NULL)
  558. goto out;
  559. msg.rpc_resp = dir_attr;
  560. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  561. nfs_post_op_update_inode(dir, dir_attr);
  562. nfs_free_fattr(dir_attr);
  563. out:
  564. dprintk("NFS reply rmdir: %d\n", status);
  565. return status;
  566. }
  567. /*
  568. * The READDIR implementation is somewhat hackish - we pass the user buffer
  569. * to the encode function, which installs it in the receive iovec.
  570. * The decode function itself doesn't perform any decoding, it just makes
  571. * sure the reply is syntactically correct.
  572. *
  573. * Also note that this implementation handles both plain readdir and
  574. * readdirplus.
  575. */
  576. static int nfs3_proc_readdir(struct nfs_readdir_arg *nr_arg,
  577. struct nfs_readdir_res *nr_res)
  578. {
  579. struct inode *dir = d_inode(nr_arg->dentry);
  580. struct nfs3_readdirargs arg = {
  581. .fh = NFS_FH(dir),
  582. .cookie = nr_arg->cookie,
  583. .plus = nr_arg->plus,
  584. .count = nr_arg->page_len,
  585. .pages = nr_arg->pages
  586. };
  587. struct nfs3_readdirres res = {
  588. .verf = nr_res->verf,
  589. .plus = nr_arg->plus,
  590. };
  591. struct rpc_message msg = {
  592. .rpc_proc = &nfs3_procedures[NFS3PROC_READDIR],
  593. .rpc_argp = &arg,
  594. .rpc_resp = &res,
  595. .rpc_cred = nr_arg->cred,
  596. };
  597. int status = -ENOMEM;
  598. if (nr_arg->plus)
  599. msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
  600. if (arg.cookie)
  601. memcpy(arg.verf, nr_arg->verf, sizeof(arg.verf));
  602. dprintk("NFS call readdir%s %llu\n", nr_arg->plus ? "plus" : "",
  603. (unsigned long long)nr_arg->cookie);
  604. res.dir_attr = nfs_alloc_fattr();
  605. if (res.dir_attr == NULL)
  606. goto out;
  607. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  608. nfs_invalidate_atime(dir);
  609. nfs_refresh_inode(dir, res.dir_attr);
  610. nfs_free_fattr(res.dir_attr);
  611. out:
  612. dprintk("NFS reply readdir%s: %d\n", nr_arg->plus ? "plus" : "",
  613. status);
  614. return status;
  615. }
  616. static int
  617. nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  618. dev_t rdev)
  619. {
  620. struct posix_acl *default_acl, *acl;
  621. struct nfs3_createdata *data;
  622. struct dentry *d_alias;
  623. int status = -ENOMEM;
  624. dprintk("NFS call mknod %pd %u:%u\n", dentry,
  625. MAJOR(rdev), MINOR(rdev));
  626. data = nfs3_alloc_createdata();
  627. if (data == NULL)
  628. goto out;
  629. status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
  630. if (status)
  631. goto out;
  632. data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
  633. data->arg.mknod.fh = NFS_FH(dir);
  634. data->arg.mknod.name = dentry->d_name.name;
  635. data->arg.mknod.len = dentry->d_name.len;
  636. data->arg.mknod.sattr = sattr;
  637. data->arg.mknod.rdev = rdev;
  638. switch (sattr->ia_mode & S_IFMT) {
  639. case S_IFBLK:
  640. data->arg.mknod.type = NF3BLK;
  641. break;
  642. case S_IFCHR:
  643. data->arg.mknod.type = NF3CHR;
  644. break;
  645. case S_IFIFO:
  646. data->arg.mknod.type = NF3FIFO;
  647. break;
  648. case S_IFSOCK:
  649. data->arg.mknod.type = NF3SOCK;
  650. break;
  651. default:
  652. status = -EINVAL;
  653. goto out_release_acls;
  654. }
  655. d_alias = nfs3_do_create(dir, dentry, data);
  656. status = PTR_ERR_OR_ZERO(d_alias);
  657. if (status != 0)
  658. goto out_release_acls;
  659. if (d_alias)
  660. dentry = d_alias;
  661. status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
  662. dput(d_alias);
  663. out_release_acls:
  664. posix_acl_release(acl);
  665. posix_acl_release(default_acl);
  666. out:
  667. nfs3_free_createdata(data);
  668. dprintk("NFS reply mknod: %d\n", status);
  669. return status;
  670. }
  671. static int
  672. nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  673. struct nfs_fsstat *stat)
  674. {
  675. struct rpc_message msg = {
  676. .rpc_proc = &nfs3_procedures[NFS3PROC_FSSTAT],
  677. .rpc_argp = fhandle,
  678. .rpc_resp = stat,
  679. };
  680. int status;
  681. dprintk("NFS call fsstat\n");
  682. nfs_fattr_init(stat->fattr);
  683. status = rpc_call_sync(server->client, &msg, 0);
  684. dprintk("NFS reply fsstat: %d\n", status);
  685. return status;
  686. }
  687. static int
  688. do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
  689. struct nfs_fsinfo *info)
  690. {
  691. struct rpc_message msg = {
  692. .rpc_proc = &nfs3_procedures[NFS3PROC_FSINFO],
  693. .rpc_argp = fhandle,
  694. .rpc_resp = info,
  695. };
  696. int status;
  697. dprintk("NFS call fsinfo\n");
  698. nfs_fattr_init(info->fattr);
  699. status = rpc_call_sync(client, &msg, 0);
  700. dprintk("NFS reply fsinfo: %d\n", status);
  701. return status;
  702. }
  703. /*
  704. * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
  705. * nfs_create_server
  706. */
  707. static int
  708. nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  709. struct nfs_fsinfo *info)
  710. {
  711. int status;
  712. status = do_proc_fsinfo(server->client, fhandle, info);
  713. if (status && server->nfs_client->cl_rpcclient != server->client)
  714. status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
  715. return status;
  716. }
  717. static int
  718. nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  719. struct nfs_pathconf *info)
  720. {
  721. struct rpc_message msg = {
  722. .rpc_proc = &nfs3_procedures[NFS3PROC_PATHCONF],
  723. .rpc_argp = fhandle,
  724. .rpc_resp = info,
  725. };
  726. int status;
  727. dprintk("NFS call pathconf\n");
  728. nfs_fattr_init(info->fattr);
  729. status = rpc_call_sync(server->client, &msg, 0);
  730. dprintk("NFS reply pathconf: %d\n", status);
  731. return status;
  732. }
  733. static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  734. {
  735. struct inode *inode = hdr->inode;
  736. struct nfs_server *server = NFS_SERVER(inode);
  737. if (hdr->pgio_done_cb != NULL)
  738. return hdr->pgio_done_cb(task, hdr);
  739. if (nfs3_async_handle_jukebox(task, inode))
  740. return -EAGAIN;
  741. if (task->tk_status >= 0 && !server->read_hdrsize)
  742. cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
  743. nfs_invalidate_atime(inode);
  744. nfs_refresh_inode(inode, &hdr->fattr);
  745. return 0;
  746. }
  747. static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
  748. struct rpc_message *msg)
  749. {
  750. msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
  751. hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
  752. }
  753. static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
  754. struct nfs_pgio_header *hdr)
  755. {
  756. rpc_call_start(task);
  757. return 0;
  758. }
  759. static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  760. {
  761. struct inode *inode = hdr->inode;
  762. if (hdr->pgio_done_cb != NULL)
  763. return hdr->pgio_done_cb(task, hdr);
  764. if (nfs3_async_handle_jukebox(task, inode))
  765. return -EAGAIN;
  766. if (task->tk_status >= 0)
  767. nfs_writeback_update_inode(hdr);
  768. return 0;
  769. }
  770. static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
  771. struct rpc_message *msg,
  772. struct rpc_clnt **clnt)
  773. {
  774. msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
  775. }
  776. static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  777. {
  778. rpc_call_start(task);
  779. }
  780. static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  781. {
  782. if (data->commit_done_cb != NULL)
  783. return data->commit_done_cb(task, data);
  784. if (nfs3_async_handle_jukebox(task, data->inode))
  785. return -EAGAIN;
  786. nfs_refresh_inode(data->inode, data->res.fattr);
  787. return 0;
  788. }
  789. static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
  790. struct rpc_clnt **clnt)
  791. {
  792. msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
  793. }
  794. static void nfs3_nlm_alloc_call(void *data)
  795. {
  796. struct nfs_lock_context *l_ctx = data;
  797. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
  798. get_nfs_open_context(l_ctx->open_context);
  799. nfs_get_lock_context(l_ctx->open_context);
  800. }
  801. }
  802. static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
  803. {
  804. struct nfs_lock_context *l_ctx = data;
  805. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
  806. return nfs_async_iocounter_wait(task, l_ctx);
  807. return false;
  808. }
  809. static void nfs3_nlm_release_call(void *data)
  810. {
  811. struct nfs_lock_context *l_ctx = data;
  812. struct nfs_open_context *ctx;
  813. if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
  814. ctx = l_ctx->open_context;
  815. nfs_put_lock_context(l_ctx);
  816. put_nfs_open_context(ctx);
  817. }
  818. }
  819. static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
  820. .nlmclnt_alloc_call = nfs3_nlm_alloc_call,
  821. .nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
  822. .nlmclnt_release_call = nfs3_nlm_release_call,
  823. };
  824. static int
  825. nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
  826. {
  827. struct inode *inode = file_inode(filp);
  828. struct nfs_lock_context *l_ctx = NULL;
  829. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  830. int status;
  831. if (fl->c.flc_flags & FL_CLOSE) {
  832. l_ctx = nfs_get_lock_context(ctx);
  833. if (IS_ERR(l_ctx))
  834. l_ctx = NULL;
  835. else
  836. set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
  837. }
  838. status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
  839. if (l_ctx)
  840. nfs_put_lock_context(l_ctx);
  841. return status;
  842. }
  843. static int nfs3_have_delegation(struct inode *inode, fmode_t type, int flags)
  844. {
  845. return 0;
  846. }
  847. static int nfs3_return_delegation(struct inode *inode)
  848. {
  849. if (S_ISREG(inode->i_mode))
  850. nfs_wb_all(inode);
  851. return 0;
  852. }
  853. static const struct inode_operations nfs3_dir_inode_operations = {
  854. .create = nfs_create,
  855. .atomic_open = nfs_atomic_open_v23,
  856. .lookup = nfs_lookup,
  857. .link = nfs_link,
  858. .unlink = nfs_unlink,
  859. .symlink = nfs_symlink,
  860. .mkdir = nfs_mkdir,
  861. .rmdir = nfs_rmdir,
  862. .mknod = nfs_mknod,
  863. .rename = nfs_rename,
  864. .permission = nfs_permission,
  865. .getattr = nfs_getattr,
  866. .setattr = nfs_setattr,
  867. #ifdef CONFIG_NFS_V3_ACL
  868. .listxattr = nfs3_listxattr,
  869. .get_inode_acl = nfs3_get_acl,
  870. .set_acl = nfs3_set_acl,
  871. #endif
  872. };
  873. static const struct inode_operations nfs3_file_inode_operations = {
  874. .permission = nfs_permission,
  875. .getattr = nfs_getattr,
  876. .setattr = nfs_setattr,
  877. #ifdef CONFIG_NFS_V3_ACL
  878. .listxattr = nfs3_listxattr,
  879. .get_inode_acl = nfs3_get_acl,
  880. .set_acl = nfs3_set_acl,
  881. #endif
  882. };
  883. const struct nfs_rpc_ops nfs_v3_clientops = {
  884. .version = 3, /* protocol version */
  885. .dentry_ops = &nfs_dentry_operations,
  886. .dir_inode_ops = &nfs3_dir_inode_operations,
  887. .file_inode_ops = &nfs3_file_inode_operations,
  888. .file_ops = &nfs_file_operations,
  889. .nlmclnt_ops = &nlmclnt_fl_close_lock_ops,
  890. .getroot = nfs3_proc_get_root,
  891. .submount = nfs_submount,
  892. .try_get_tree = nfs_try_get_tree,
  893. .getattr = nfs3_proc_getattr,
  894. .setattr = nfs3_proc_setattr,
  895. .lookup = nfs3_proc_lookup,
  896. .lookupp = nfs3_proc_lookupp,
  897. .access = nfs3_proc_access,
  898. .readlink = nfs3_proc_readlink,
  899. .create = nfs3_proc_create,
  900. .remove = nfs3_proc_remove,
  901. .unlink_setup = nfs3_proc_unlink_setup,
  902. .unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
  903. .unlink_done = nfs3_proc_unlink_done,
  904. .rename_setup = nfs3_proc_rename_setup,
  905. .rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
  906. .rename_done = nfs3_proc_rename_done,
  907. .link = nfs3_proc_link,
  908. .symlink = nfs3_proc_symlink,
  909. .mkdir = nfs3_proc_mkdir,
  910. .rmdir = nfs3_proc_rmdir,
  911. .readdir = nfs3_proc_readdir,
  912. .mknod = nfs3_proc_mknod,
  913. .statfs = nfs3_proc_statfs,
  914. .fsinfo = nfs3_proc_fsinfo,
  915. .pathconf = nfs3_proc_pathconf,
  916. .decode_dirent = nfs3_decode_dirent,
  917. .pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
  918. .read_setup = nfs3_proc_read_setup,
  919. .read_done = nfs3_read_done,
  920. .write_setup = nfs3_proc_write_setup,
  921. .write_done = nfs3_write_done,
  922. .commit_setup = nfs3_proc_commit_setup,
  923. .commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
  924. .commit_done = nfs3_commit_done,
  925. .lock = nfs3_proc_lock,
  926. .clear_acl_cache = forget_all_cached_acls,
  927. .close_context = nfs_close_context,
  928. .have_delegation = nfs3_have_delegation,
  929. .return_delegation = nfs3_return_delegation,
  930. .alloc_client = nfs_alloc_client,
  931. .init_client = nfs_init_client,
  932. .free_client = nfs_free_client,
  933. .create_server = nfs3_create_server,
  934. .clone_server = nfs3_clone_server,
  935. };