nfs3proc.c 25 KB

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