nfs3proc.c 25 KB

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