nfs3proc.c 25 KB

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