proc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/nfs/proc.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994 Rick Sladkey
  6. *
  7. * OS-independent nfs remote procedure call functions
  8. *
  9. * Tuned by Alan Cox <A.Cox@swansea.ac.uk> for >3K buffers
  10. * so at last we can have decent(ish) throughput off a
  11. * Sun server.
  12. *
  13. * Coding optimized and cleaned up by Florian La Roche.
  14. * Note: Error returns are optimized for NFS_OK, which isn't translated via
  15. * nfs_stat_to_errno(), but happens to be already the right return code.
  16. *
  17. * Also, the code currently doesn't check the size of the packet, when
  18. * it decodes the packet.
  19. *
  20. * Feel free to fix it and mail me the diffs if it worries you.
  21. *
  22. * Completely rewritten to support the new RPC call interface;
  23. * rewrote and moved the entire XDR stuff to xdr.c
  24. * --Olaf Kirch June 1996
  25. *
  26. * The code below initializes all auto variables explicitly, otherwise
  27. * it will fail to work as a module (gcc generates a memset call for an
  28. * incomplete struct).
  29. */
  30. #include <linux/types.h>
  31. #include <linux/param.h>
  32. #include <linux/time.h>
  33. #include <linux/mm.h>
  34. #include <linux/errno.h>
  35. #include <linux/string.h>
  36. #include <linux/in.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/sunrpc/clnt.h>
  39. #include <linux/nfs.h>
  40. #include <linux/nfs2.h>
  41. #include <linux/nfs_fs.h>
  42. #include <linux/nfs_page.h>
  43. #include <linux/lockd/bind.h>
  44. #include <linux/freezer.h>
  45. #include "internal.h"
  46. #define NFSDBG_FACILITY NFSDBG_PROC
  47. /*
  48. * Bare-bones access to getattr: this is for nfs_read_super.
  49. */
  50. static int
  51. nfs_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  52. struct nfs_fsinfo *info)
  53. {
  54. struct nfs_fattr *fattr = info->fattr;
  55. struct nfs2_fsstat fsinfo;
  56. struct rpc_message msg = {
  57. .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
  58. .rpc_argp = fhandle,
  59. .rpc_resp = fattr,
  60. };
  61. int status;
  62. dprintk("%s: call getattr\n", __func__);
  63. nfs_fattr_init(fattr);
  64. status = rpc_call_sync(server->client, &msg, 0);
  65. /* Retry with default authentication if different */
  66. if (status && server->nfs_client->cl_rpcclient != server->client)
  67. status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
  68. dprintk("%s: reply getattr: %d\n", __func__, status);
  69. if (status)
  70. return status;
  71. dprintk("%s: call statfs\n", __func__);
  72. msg.rpc_proc = &nfs_procedures[NFSPROC_STATFS];
  73. msg.rpc_resp = &fsinfo;
  74. status = rpc_call_sync(server->client, &msg, 0);
  75. /* Retry with default authentication if different */
  76. if (status && server->nfs_client->cl_rpcclient != server->client)
  77. status = rpc_call_sync(server->nfs_client->cl_rpcclient, &msg, 0);
  78. dprintk("%s: reply statfs: %d\n", __func__, status);
  79. if (status)
  80. return status;
  81. info->rtmax = NFS_MAXDATA;
  82. info->rtpref = fsinfo.tsize;
  83. info->rtmult = fsinfo.bsize;
  84. info->wtmax = NFS_MAXDATA;
  85. info->wtpref = fsinfo.tsize;
  86. info->wtmult = fsinfo.bsize;
  87. info->dtpref = fsinfo.tsize;
  88. info->maxfilesize = 0x7FFFFFFF;
  89. info->lease_time = 0;
  90. info->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
  91. info->xattr_support = 0;
  92. return 0;
  93. }
  94. /*
  95. * One function for each procedure in the NFS protocol.
  96. */
  97. static int
  98. nfs_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  99. struct nfs_fattr *fattr, struct inode *inode)
  100. {
  101. struct rpc_message msg = {
  102. .rpc_proc = &nfs_procedures[NFSPROC_GETATTR],
  103. .rpc_argp = fhandle,
  104. .rpc_resp = fattr,
  105. };
  106. int status;
  107. unsigned short task_flags = 0;
  108. /* Is this is an attribute revalidation, subject to softreval? */
  109. if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
  110. task_flags |= RPC_TASK_TIMEOUT;
  111. dprintk("NFS call getattr\n");
  112. nfs_fattr_init(fattr);
  113. status = rpc_call_sync(server->client, &msg, task_flags);
  114. dprintk("NFS reply getattr: %d\n", status);
  115. return status;
  116. }
  117. static int
  118. nfs_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  119. struct iattr *sattr)
  120. {
  121. struct inode *inode = d_inode(dentry);
  122. struct nfs_sattrargs arg = {
  123. .fh = NFS_FH(inode),
  124. .sattr = sattr
  125. };
  126. struct rpc_message msg = {
  127. .rpc_proc = &nfs_procedures[NFSPROC_SETATTR],
  128. .rpc_argp = &arg,
  129. .rpc_resp = fattr,
  130. };
  131. int status;
  132. /* Mask out the non-modebit related stuff from attr->ia_mode */
  133. sattr->ia_mode &= S_IALLUGO;
  134. dprintk("NFS call setattr\n");
  135. if (sattr->ia_valid & ATTR_FILE)
  136. msg.rpc_cred = nfs_file_cred(sattr->ia_file);
  137. nfs_fattr_init(fattr);
  138. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  139. if (status == 0)
  140. nfs_setattr_update_inode(inode, sattr, fattr);
  141. dprintk("NFS reply setattr: %d\n", status);
  142. return status;
  143. }
  144. static int
  145. nfs_proc_lookup(struct inode *dir, struct dentry *dentry, const struct qstr *name,
  146. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  147. {
  148. struct nfs_diropargs arg = {
  149. .fh = NFS_FH(dir),
  150. .name = name->name,
  151. .len = name->len
  152. };
  153. struct nfs_diropok res = {
  154. .fh = fhandle,
  155. .fattr = fattr
  156. };
  157. struct rpc_message msg = {
  158. .rpc_proc = &nfs_procedures[NFSPROC_LOOKUP],
  159. .rpc_argp = &arg,
  160. .rpc_resp = &res,
  161. };
  162. int status;
  163. unsigned short task_flags = 0;
  164. /* Is this is an attribute revalidation, subject to softreval? */
  165. if (nfs_lookup_is_soft_revalidate(dentry))
  166. task_flags |= RPC_TASK_TIMEOUT;
  167. dprintk("NFS call lookup %pd2\n", dentry);
  168. nfs_fattr_init(fattr);
  169. status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
  170. dprintk("NFS reply lookup: %d\n", status);
  171. return status;
  172. }
  173. static int nfs_proc_readlink(struct inode *inode, struct page *page,
  174. unsigned int pgbase, unsigned int pglen)
  175. {
  176. struct nfs_readlinkargs args = {
  177. .fh = NFS_FH(inode),
  178. .pgbase = pgbase,
  179. .pglen = pglen,
  180. .pages = &page
  181. };
  182. struct rpc_message msg = {
  183. .rpc_proc = &nfs_procedures[NFSPROC_READLINK],
  184. .rpc_argp = &args,
  185. };
  186. int status;
  187. dprintk("NFS call readlink\n");
  188. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  189. dprintk("NFS reply readlink: %d\n", status);
  190. return status;
  191. }
  192. struct nfs_createdata {
  193. struct nfs_createargs arg;
  194. struct nfs_diropok res;
  195. struct nfs_fh fhandle;
  196. struct nfs_fattr fattr;
  197. };
  198. static struct nfs_createdata *nfs_alloc_createdata(struct inode *dir,
  199. struct dentry *dentry, struct iattr *sattr)
  200. {
  201. struct nfs_createdata *data;
  202. data = kmalloc_obj(*data);
  203. if (data != NULL) {
  204. data->arg.fh = NFS_FH(dir);
  205. data->arg.name = dentry->d_name.name;
  206. data->arg.len = dentry->d_name.len;
  207. data->arg.sattr = sattr;
  208. nfs_fattr_init(&data->fattr);
  209. data->fhandle.size = 0;
  210. data->res.fh = &data->fhandle;
  211. data->res.fattr = &data->fattr;
  212. }
  213. return data;
  214. };
  215. static void nfs_free_createdata(const struct nfs_createdata *data)
  216. {
  217. kfree(data);
  218. }
  219. static int
  220. nfs_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  221. int flags)
  222. {
  223. struct nfs_createdata *data;
  224. struct rpc_message msg = {
  225. .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
  226. };
  227. int status = -ENOMEM;
  228. dprintk("NFS call create %pd\n", dentry);
  229. data = nfs_alloc_createdata(dir, dentry, sattr);
  230. if (data == NULL)
  231. goto out;
  232. msg.rpc_argp = &data->arg;
  233. msg.rpc_resp = &data->res;
  234. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  235. nfs_mark_for_revalidate(dir);
  236. if (status == 0)
  237. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  238. nfs_free_createdata(data);
  239. out:
  240. dprintk("NFS reply create: %d\n", status);
  241. return status;
  242. }
  243. /*
  244. * In NFSv2, mknod is grafted onto the create call.
  245. */
  246. static int
  247. nfs_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  248. dev_t rdev)
  249. {
  250. struct nfs_createdata *data;
  251. struct rpc_message msg = {
  252. .rpc_proc = &nfs_procedures[NFSPROC_CREATE],
  253. };
  254. umode_t mode;
  255. int status = -ENOMEM;
  256. dprintk("NFS call mknod %pd\n", dentry);
  257. mode = sattr->ia_mode;
  258. if (S_ISFIFO(mode)) {
  259. sattr->ia_mode = (mode & ~S_IFMT) | S_IFCHR;
  260. sattr->ia_valid &= ~ATTR_SIZE;
  261. } else if (S_ISCHR(mode) || S_ISBLK(mode)) {
  262. sattr->ia_valid |= ATTR_SIZE;
  263. sattr->ia_size = new_encode_dev(rdev);/* get out your barf bag */
  264. }
  265. data = nfs_alloc_createdata(dir, dentry, sattr);
  266. if (data == NULL)
  267. goto out;
  268. msg.rpc_argp = &data->arg;
  269. msg.rpc_resp = &data->res;
  270. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  271. nfs_mark_for_revalidate(dir);
  272. if (status == -EINVAL && S_ISFIFO(mode)) {
  273. sattr->ia_mode = mode;
  274. nfs_fattr_init(data->res.fattr);
  275. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  276. }
  277. if (status == 0)
  278. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  279. nfs_free_createdata(data);
  280. out:
  281. dprintk("NFS reply mknod: %d\n", status);
  282. return status;
  283. }
  284. static int
  285. nfs_proc_remove(struct inode *dir, struct dentry *dentry)
  286. {
  287. struct nfs_removeargs arg = {
  288. .fh = NFS_FH(dir),
  289. .name = dentry->d_name,
  290. };
  291. struct rpc_message msg = {
  292. .rpc_proc = &nfs_procedures[NFSPROC_REMOVE],
  293. .rpc_argp = &arg,
  294. };
  295. int status;
  296. dprintk("NFS call remove %pd2\n",dentry);
  297. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  298. nfs_mark_for_revalidate(dir);
  299. dprintk("NFS reply remove: %d\n", status);
  300. return status;
  301. }
  302. static void
  303. nfs_proc_unlink_setup(struct rpc_message *msg,
  304. struct dentry *dentry,
  305. struct inode *inode)
  306. {
  307. msg->rpc_proc = &nfs_procedures[NFSPROC_REMOVE];
  308. }
  309. static void nfs_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  310. {
  311. rpc_call_start(task);
  312. }
  313. static int nfs_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  314. {
  315. nfs_mark_for_revalidate(dir);
  316. return 1;
  317. }
  318. static void
  319. nfs_proc_rename_setup(struct rpc_message *msg,
  320. struct dentry *old_dentry,
  321. struct dentry *new_dentry,
  322. struct inode *same_parent)
  323. {
  324. msg->rpc_proc = &nfs_procedures[NFSPROC_RENAME];
  325. }
  326. static void nfs_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  327. {
  328. rpc_call_start(task);
  329. }
  330. static int
  331. nfs_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  332. struct inode *new_dir)
  333. {
  334. nfs_mark_for_revalidate(old_dir);
  335. nfs_mark_for_revalidate(new_dir);
  336. return 1;
  337. }
  338. static int
  339. nfs_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  340. {
  341. struct nfs_linkargs arg = {
  342. .fromfh = NFS_FH(inode),
  343. .tofh = NFS_FH(dir),
  344. .toname = name->name,
  345. .tolen = name->len
  346. };
  347. struct rpc_message msg = {
  348. .rpc_proc = &nfs_procedures[NFSPROC_LINK],
  349. .rpc_argp = &arg,
  350. };
  351. int status;
  352. dprintk("NFS call link %s\n", name->name);
  353. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  354. nfs_mark_for_revalidate(inode);
  355. nfs_mark_for_revalidate(dir);
  356. dprintk("NFS reply link: %d\n", status);
  357. return status;
  358. }
  359. static int
  360. nfs_proc_symlink(struct inode *dir, struct dentry *dentry, struct folio *folio,
  361. unsigned int len, struct iattr *sattr)
  362. {
  363. struct page *page = &folio->page;
  364. struct nfs_fh *fh;
  365. struct nfs_fattr *fattr;
  366. struct nfs_symlinkargs arg = {
  367. .fromfh = NFS_FH(dir),
  368. .fromname = dentry->d_name.name,
  369. .fromlen = dentry->d_name.len,
  370. .pages = &page,
  371. .pathlen = len,
  372. .sattr = sattr
  373. };
  374. struct rpc_message msg = {
  375. .rpc_proc = &nfs_procedures[NFSPROC_SYMLINK],
  376. .rpc_argp = &arg,
  377. };
  378. int status = -ENAMETOOLONG;
  379. dprintk("NFS call symlink %pd\n", dentry);
  380. if (len > NFS2_MAXPATHLEN)
  381. goto out;
  382. fh = nfs_alloc_fhandle();
  383. fattr = nfs_alloc_fattr();
  384. status = -ENOMEM;
  385. if (fh == NULL || fattr == NULL)
  386. goto out_free;
  387. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  388. nfs_mark_for_revalidate(dir);
  389. /*
  390. * V2 SYMLINK requests don't return any attributes. Setting the
  391. * filehandle size to zero indicates to nfs_instantiate that it
  392. * should fill in the data with a LOOKUP call on the wire.
  393. */
  394. if (status == 0)
  395. status = nfs_instantiate(dentry, fh, fattr);
  396. out_free:
  397. nfs_free_fattr(fattr);
  398. nfs_free_fhandle(fh);
  399. out:
  400. dprintk("NFS reply symlink: %d\n", status);
  401. return status;
  402. }
  403. static struct dentry *
  404. nfs_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
  405. {
  406. struct nfs_createdata *data;
  407. struct rpc_message msg = {
  408. .rpc_proc = &nfs_procedures[NFSPROC_MKDIR],
  409. };
  410. struct dentry *alias = NULL;
  411. int status = -ENOMEM;
  412. dprintk("NFS call mkdir %pd\n", dentry);
  413. data = nfs_alloc_createdata(dir, dentry, sattr);
  414. if (data == NULL)
  415. goto out;
  416. msg.rpc_argp = &data->arg;
  417. msg.rpc_resp = &data->res;
  418. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  419. nfs_mark_for_revalidate(dir);
  420. if (status == 0) {
  421. alias = nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
  422. status = PTR_ERR_OR_ZERO(alias);
  423. } else
  424. alias = ERR_PTR(status);
  425. nfs_free_createdata(data);
  426. out:
  427. dprintk("NFS reply mkdir: %d\n", status);
  428. return alias;
  429. }
  430. static int
  431. nfs_proc_rmdir(struct inode *dir, const struct qstr *name)
  432. {
  433. struct nfs_diropargs arg = {
  434. .fh = NFS_FH(dir),
  435. .name = name->name,
  436. .len = name->len
  437. };
  438. struct rpc_message msg = {
  439. .rpc_proc = &nfs_procedures[NFSPROC_RMDIR],
  440. .rpc_argp = &arg,
  441. };
  442. int status;
  443. dprintk("NFS call rmdir %s\n", name->name);
  444. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  445. nfs_mark_for_revalidate(dir);
  446. dprintk("NFS reply rmdir: %d\n", status);
  447. return status;
  448. }
  449. /*
  450. * The READDIR implementation is somewhat hackish - we pass a temporary
  451. * buffer to the encode function, which installs it in the receive
  452. * the receive iovec. The decode function just parses the reply to make
  453. * sure it is syntactically correct; the entries itself are decoded
  454. * from nfs_readdir by calling the decode_entry function directly.
  455. */
  456. static int nfs_proc_readdir(struct nfs_readdir_arg *nr_arg,
  457. struct nfs_readdir_res *nr_res)
  458. {
  459. struct inode *dir = d_inode(nr_arg->dentry);
  460. struct nfs_readdirargs arg = {
  461. .fh = NFS_FH(dir),
  462. .cookie = nr_arg->cookie,
  463. .count = nr_arg->page_len,
  464. .pages = nr_arg->pages,
  465. };
  466. struct rpc_message msg = {
  467. .rpc_proc = &nfs_procedures[NFSPROC_READDIR],
  468. .rpc_argp = &arg,
  469. .rpc_cred = nr_arg->cred,
  470. };
  471. int status;
  472. dprintk("NFS call readdir %llu\n", (unsigned long long)nr_arg->cookie);
  473. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  474. nr_res->verf[0] = nr_res->verf[1] = 0;
  475. nfs_invalidate_atime(dir);
  476. dprintk("NFS reply readdir: %d\n", status);
  477. return status;
  478. }
  479. static int
  480. nfs_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  481. struct nfs_fsstat *stat)
  482. {
  483. struct nfs2_fsstat fsinfo;
  484. struct rpc_message msg = {
  485. .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
  486. .rpc_argp = fhandle,
  487. .rpc_resp = &fsinfo,
  488. };
  489. int status;
  490. dprintk("NFS call statfs\n");
  491. nfs_fattr_init(stat->fattr);
  492. status = rpc_call_sync(server->client, &msg, 0);
  493. dprintk("NFS reply statfs: %d\n", status);
  494. if (status)
  495. goto out;
  496. stat->tbytes = (u64)fsinfo.blocks * fsinfo.bsize;
  497. stat->fbytes = (u64)fsinfo.bfree * fsinfo.bsize;
  498. stat->abytes = (u64)fsinfo.bavail * fsinfo.bsize;
  499. stat->tfiles = 0;
  500. stat->ffiles = 0;
  501. stat->afiles = 0;
  502. out:
  503. return status;
  504. }
  505. static int
  506. nfs_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  507. struct nfs_fsinfo *info)
  508. {
  509. struct nfs2_fsstat fsinfo;
  510. struct rpc_message msg = {
  511. .rpc_proc = &nfs_procedures[NFSPROC_STATFS],
  512. .rpc_argp = fhandle,
  513. .rpc_resp = &fsinfo,
  514. };
  515. int status;
  516. dprintk("NFS call fsinfo\n");
  517. nfs_fattr_init(info->fattr);
  518. status = rpc_call_sync(server->client, &msg, 0);
  519. dprintk("NFS reply fsinfo: %d\n", status);
  520. if (status)
  521. goto out;
  522. info->rtmax = NFS_MAXDATA;
  523. info->rtpref = fsinfo.tsize;
  524. info->rtmult = fsinfo.bsize;
  525. info->wtmax = NFS_MAXDATA;
  526. info->wtpref = fsinfo.tsize;
  527. info->wtmult = fsinfo.bsize;
  528. info->dtpref = fsinfo.tsize;
  529. info->maxfilesize = 0x7FFFFFFF;
  530. info->lease_time = 0;
  531. out:
  532. return status;
  533. }
  534. static int
  535. nfs_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  536. struct nfs_pathconf *info)
  537. {
  538. info->max_link = 0;
  539. info->max_namelen = NFS2_MAXNAMLEN;
  540. return 0;
  541. }
  542. static int nfs_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  543. {
  544. struct inode *inode = hdr->inode;
  545. nfs_invalidate_atime(inode);
  546. if (task->tk_status >= 0) {
  547. nfs_refresh_inode(inode, hdr->res.fattr);
  548. /* Emulate the eof flag, which isn't normally needed in NFSv2
  549. * as it is guaranteed to always return the file attributes
  550. */
  551. if ((hdr->res.count == 0 && hdr->args.count > 0) ||
  552. hdr->args.offset + hdr->res.count >= hdr->res.fattr->size)
  553. hdr->res.eof = 1;
  554. }
  555. return 0;
  556. }
  557. static void nfs_proc_read_setup(struct nfs_pgio_header *hdr,
  558. struct rpc_message *msg)
  559. {
  560. msg->rpc_proc = &nfs_procedures[NFSPROC_READ];
  561. }
  562. static int nfs_proc_pgio_rpc_prepare(struct rpc_task *task,
  563. struct nfs_pgio_header *hdr)
  564. {
  565. rpc_call_start(task);
  566. return 0;
  567. }
  568. static int nfs_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  569. {
  570. if (task->tk_status >= 0) {
  571. hdr->res.count = hdr->args.count;
  572. nfs_writeback_update_inode(hdr);
  573. }
  574. return 0;
  575. }
  576. static void nfs_proc_write_setup(struct nfs_pgio_header *hdr,
  577. struct rpc_message *msg,
  578. struct rpc_clnt **clnt)
  579. {
  580. /* Note: NFSv2 ignores @stable and always uses NFS_FILE_SYNC */
  581. hdr->args.stable = NFS_FILE_SYNC;
  582. msg->rpc_proc = &nfs_procedures[NFSPROC_WRITE];
  583. }
  584. static void nfs_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  585. {
  586. BUG();
  587. }
  588. static void
  589. nfs_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
  590. struct rpc_clnt **clnt)
  591. {
  592. BUG();
  593. }
  594. static int
  595. nfs_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
  596. {
  597. struct inode *inode = file_inode(filp);
  598. return nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, NULL);
  599. }
  600. /* Helper functions for NFS lock bounds checking */
  601. #define NFS_LOCK32_OFFSET_MAX ((__s32)0x7fffffffUL)
  602. static int nfs_lock_check_bounds(const struct file_lock *fl)
  603. {
  604. __s32 start, end;
  605. start = (__s32)fl->fl_start;
  606. if ((loff_t)start != fl->fl_start)
  607. goto out_einval;
  608. if (fl->fl_end != OFFSET_MAX) {
  609. end = (__s32)fl->fl_end;
  610. if ((loff_t)end != fl->fl_end)
  611. goto out_einval;
  612. } else
  613. end = NFS_LOCK32_OFFSET_MAX;
  614. if (start < 0 || start > end)
  615. goto out_einval;
  616. return 0;
  617. out_einval:
  618. return -EINVAL;
  619. }
  620. static int nfs_have_delegation(struct inode *inode, fmode_t type, int flags)
  621. {
  622. return 0;
  623. }
  624. static void nfs_return_delegation(struct inode *inode)
  625. {
  626. if (S_ISREG(inode->i_mode))
  627. nfs_wb_all(inode);
  628. }
  629. static const struct inode_operations nfs_dir_inode_operations = {
  630. .create = nfs_create,
  631. .lookup = nfs_lookup,
  632. .atomic_open = nfs_atomic_open_v23,
  633. .link = nfs_link,
  634. .unlink = nfs_unlink,
  635. .symlink = nfs_symlink,
  636. .mkdir = nfs_mkdir,
  637. .rmdir = nfs_rmdir,
  638. .mknod = nfs_mknod,
  639. .rename = nfs_rename,
  640. .permission = nfs_permission,
  641. .getattr = nfs_getattr,
  642. .setattr = nfs_setattr,
  643. };
  644. static const struct inode_operations nfs_file_inode_operations = {
  645. .permission = nfs_permission,
  646. .getattr = nfs_getattr,
  647. .setattr = nfs_setattr,
  648. };
  649. const struct nfs_rpc_ops nfs_v2_clientops = {
  650. .version = 2, /* protocol version */
  651. .dentry_ops = &nfs_dentry_operations,
  652. .dir_inode_ops = &nfs_dir_inode_operations,
  653. .file_inode_ops = &nfs_file_inode_operations,
  654. .file_ops = &nfs_file_operations,
  655. .getroot = nfs_proc_get_root,
  656. .submount = nfs_submount,
  657. .try_get_tree = nfs_try_get_tree,
  658. .getattr = nfs_proc_getattr,
  659. .setattr = nfs_proc_setattr,
  660. .lookup = nfs_proc_lookup,
  661. .access = NULL, /* access */
  662. .readlink = nfs_proc_readlink,
  663. .create = nfs_proc_create,
  664. .remove = nfs_proc_remove,
  665. .unlink_setup = nfs_proc_unlink_setup,
  666. .unlink_rpc_prepare = nfs_proc_unlink_rpc_prepare,
  667. .unlink_done = nfs_proc_unlink_done,
  668. .rename_setup = nfs_proc_rename_setup,
  669. .rename_rpc_prepare = nfs_proc_rename_rpc_prepare,
  670. .rename_done = nfs_proc_rename_done,
  671. .link = nfs_proc_link,
  672. .symlink = nfs_proc_symlink,
  673. .mkdir = nfs_proc_mkdir,
  674. .rmdir = nfs_proc_rmdir,
  675. .readdir = nfs_proc_readdir,
  676. .mknod = nfs_proc_mknod,
  677. .statfs = nfs_proc_statfs,
  678. .fsinfo = nfs_proc_fsinfo,
  679. .pathconf = nfs_proc_pathconf,
  680. .decode_dirent = nfs2_decode_dirent,
  681. .pgio_rpc_prepare = nfs_proc_pgio_rpc_prepare,
  682. .read_setup = nfs_proc_read_setup,
  683. .read_done = nfs_read_done,
  684. .write_setup = nfs_proc_write_setup,
  685. .write_done = nfs_write_done,
  686. .commit_setup = nfs_proc_commit_setup,
  687. .commit_rpc_prepare = nfs_proc_commit_rpc_prepare,
  688. .lock = nfs_proc_lock,
  689. .lock_check_bounds = nfs_lock_check_bounds,
  690. .close_context = nfs_close_context,
  691. .have_delegation = nfs_have_delegation,
  692. .return_delegation = nfs_return_delegation,
  693. .alloc_client = nfs_alloc_client,
  694. .init_client = nfs_init_client,
  695. .free_client = nfs_free_client,
  696. .create_server = nfs_create_server,
  697. .clone_server = nfs_clone_server,
  698. };