nfs3proc.c 25 KB

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