vfs.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * File operations used by nfsd. Some of these have been ripped from
  4. * other parts of the kernel because they weren't exported, others
  5. * are partial duplicates with added or changed functionality.
  6. *
  7. * Note that several functions dget() the dentry upon which they want
  8. * to act, most notably those that create directory entries. Response
  9. * dentry's are dput()'d if necessary in the release callback.
  10. * So if you notice code paths that apparently fail to dput() the
  11. * dentry, don't worry--they have been taken care of.
  12. *
  13. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  14. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  15. */
  16. #include <linux/fs.h>
  17. #include <linux/file.h>
  18. #include <linux/splice.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/namei.h>
  22. #include <linux/delay.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/xattr.h>
  26. #include <linux/jhash.h>
  27. #include <linux/ima.h>
  28. #include <linux/slab.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/writeback.h>
  32. #include <linux/security.h>
  33. #ifdef CONFIG_NFSD_V3
  34. #include "xdr3.h"
  35. #endif /* CONFIG_NFSD_V3 */
  36. #ifdef CONFIG_NFSD_V4
  37. #include "../internal.h"
  38. #include "acl.h"
  39. #include "idmap.h"
  40. #endif /* CONFIG_NFSD_V4 */
  41. #include "nfsd.h"
  42. #include "vfs.h"
  43. #include "trace.h"
  44. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  45. /*
  46. * This is a cache of readahead params that help us choose the proper
  47. * readahead strategy. Initially, we set all readahead parameters to 0
  48. * and let the VFS handle things.
  49. * If you increase the number of cached files very much, you'll need to
  50. * add a hash table here.
  51. */
  52. struct raparms {
  53. struct raparms *p_next;
  54. unsigned int p_count;
  55. ino_t p_ino;
  56. dev_t p_dev;
  57. int p_set;
  58. struct file_ra_state p_ra;
  59. unsigned int p_hindex;
  60. };
  61. struct raparm_hbucket {
  62. struct raparms *pb_head;
  63. spinlock_t pb_lock;
  64. } ____cacheline_aligned_in_smp;
  65. #define RAPARM_HASH_BITS 4
  66. #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
  67. #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
  68. static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
  69. /*
  70. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  71. * a mount point.
  72. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  73. * or nfs_ok having possibly changed *dpp and *expp
  74. */
  75. int
  76. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  77. struct svc_export **expp)
  78. {
  79. struct svc_export *exp = *expp, *exp2 = NULL;
  80. struct dentry *dentry = *dpp;
  81. struct path path = {.mnt = mntget(exp->ex_path.mnt),
  82. .dentry = dget(dentry)};
  83. int err = 0;
  84. err = follow_down(&path);
  85. if (err < 0)
  86. goto out;
  87. if (path.mnt == exp->ex_path.mnt && path.dentry == dentry &&
  88. nfsd_mountpoint(dentry, exp) == 2) {
  89. /* This is only a mountpoint in some other namespace */
  90. path_put(&path);
  91. goto out;
  92. }
  93. exp2 = rqst_exp_get_by_name(rqstp, &path);
  94. if (IS_ERR(exp2)) {
  95. err = PTR_ERR(exp2);
  96. /*
  97. * We normally allow NFS clients to continue
  98. * "underneath" a mountpoint that is not exported.
  99. * The exception is V4ROOT, where no traversal is ever
  100. * allowed without an explicit export of the new
  101. * directory.
  102. */
  103. if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
  104. err = 0;
  105. path_put(&path);
  106. goto out;
  107. }
  108. if (nfsd_v4client(rqstp) ||
  109. (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  110. /* successfully crossed mount point */
  111. /*
  112. * This is subtle: path.dentry is *not* on path.mnt
  113. * at this point. The only reason we are safe is that
  114. * original mnt is pinned down by exp, so we should
  115. * put path *before* putting exp
  116. */
  117. *dpp = path.dentry;
  118. path.dentry = dentry;
  119. *expp = exp2;
  120. exp2 = exp;
  121. }
  122. path_put(&path);
  123. exp_put(exp2);
  124. out:
  125. return err;
  126. }
  127. static void follow_to_parent(struct path *path)
  128. {
  129. struct dentry *dp;
  130. while (path->dentry == path->mnt->mnt_root && follow_up(path))
  131. ;
  132. dp = dget_parent(path->dentry);
  133. dput(path->dentry);
  134. path->dentry = dp;
  135. }
  136. static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
  137. {
  138. struct svc_export *exp2;
  139. struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
  140. .dentry = dget(dparent)};
  141. follow_to_parent(&path);
  142. exp2 = rqst_exp_parent(rqstp, &path);
  143. if (PTR_ERR(exp2) == -ENOENT) {
  144. *dentryp = dget(dparent);
  145. } else if (IS_ERR(exp2)) {
  146. path_put(&path);
  147. return PTR_ERR(exp2);
  148. } else {
  149. *dentryp = dget(path.dentry);
  150. exp_put(*exp);
  151. *exp = exp2;
  152. }
  153. path_put(&path);
  154. return 0;
  155. }
  156. /*
  157. * For nfsd purposes, we treat V4ROOT exports as though there was an
  158. * export at *every* directory.
  159. * We return:
  160. * '1' if this dentry *must* be an export point,
  161. * '2' if it might be, if there is really a mount here, and
  162. * '0' if there is no chance of an export point here.
  163. */
  164. int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
  165. {
  166. if (!d_inode(dentry))
  167. return 0;
  168. if (exp->ex_flags & NFSEXP_V4ROOT)
  169. return 1;
  170. if (nfsd4_is_junction(dentry))
  171. return 1;
  172. if (d_mountpoint(dentry))
  173. /*
  174. * Might only be a mountpoint in a different namespace,
  175. * but we need to check.
  176. */
  177. return 2;
  178. return 0;
  179. }
  180. __be32
  181. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  182. const char *name, unsigned int len,
  183. struct svc_export **exp_ret, struct dentry **dentry_ret)
  184. {
  185. struct svc_export *exp;
  186. struct dentry *dparent;
  187. struct dentry *dentry;
  188. int host_err;
  189. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  190. dparent = fhp->fh_dentry;
  191. exp = exp_get(fhp->fh_export);
  192. /* Lookup the name, but don't follow links */
  193. if (isdotent(name, len)) {
  194. if (len==1)
  195. dentry = dget(dparent);
  196. else if (dparent != exp->ex_path.dentry)
  197. dentry = dget_parent(dparent);
  198. else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
  199. dentry = dget(dparent); /* .. == . just like at / */
  200. else {
  201. /* checking mountpoint crossing is very different when stepping up */
  202. host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
  203. if (host_err)
  204. goto out_nfserr;
  205. }
  206. } else {
  207. /*
  208. * In the nfsd4_open() case, this may be held across
  209. * subsequent open and delegation acquisition which may
  210. * need to take the child's i_mutex:
  211. */
  212. fh_lock_nested(fhp, I_MUTEX_PARENT);
  213. dentry = lookup_one_len(name, dparent, len);
  214. host_err = PTR_ERR(dentry);
  215. if (IS_ERR(dentry))
  216. goto out_nfserr;
  217. if (nfsd_mountpoint(dentry, exp)) {
  218. /*
  219. * We don't need the i_mutex after all. It's
  220. * still possible we could open this (regular
  221. * files can be mountpoints too), but the
  222. * i_mutex is just there to prevent renames of
  223. * something that we might be about to delegate,
  224. * and a mountpoint won't be renamed:
  225. */
  226. fh_unlock(fhp);
  227. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  228. dput(dentry);
  229. goto out_nfserr;
  230. }
  231. }
  232. }
  233. *dentry_ret = dentry;
  234. *exp_ret = exp;
  235. return 0;
  236. out_nfserr:
  237. exp_put(exp);
  238. return nfserrno(host_err);
  239. }
  240. /*
  241. * Look up one component of a pathname.
  242. * N.B. After this call _both_ fhp and resfh need an fh_put
  243. *
  244. * If the lookup would cross a mountpoint, and the mounted filesystem
  245. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  246. * accepted as it stands and the mounted directory is
  247. * returned. Otherwise the covered directory is returned.
  248. * NOTE: this mountpoint crossing is not supported properly by all
  249. * clients and is explicitly disallowed for NFSv3
  250. * NeilBrown <neilb@cse.unsw.edu.au>
  251. */
  252. __be32
  253. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  254. unsigned int len, struct svc_fh *resfh)
  255. {
  256. struct svc_export *exp;
  257. struct dentry *dentry;
  258. __be32 err;
  259. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  260. if (err)
  261. return err;
  262. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  263. if (err)
  264. return err;
  265. err = check_nfsd_access(exp, rqstp);
  266. if (err)
  267. goto out;
  268. /*
  269. * Note: we compose the file handle now, but as the
  270. * dentry may be negative, it may need to be updated.
  271. */
  272. err = fh_compose(resfh, exp, dentry, fhp);
  273. if (!err && d_really_is_negative(dentry))
  274. err = nfserr_noent;
  275. out:
  276. dput(dentry);
  277. exp_put(exp);
  278. return err;
  279. }
  280. /*
  281. * Commit metadata changes to stable storage.
  282. */
  283. static int
  284. commit_metadata(struct svc_fh *fhp)
  285. {
  286. struct inode *inode = d_inode(fhp->fh_dentry);
  287. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  288. if (!EX_ISSYNC(fhp->fh_export))
  289. return 0;
  290. if (export_ops->commit_metadata)
  291. return export_ops->commit_metadata(inode);
  292. return sync_inode_metadata(inode, 1);
  293. }
  294. /*
  295. * Go over the attributes and take care of the small differences between
  296. * NFS semantics and what Linux expects.
  297. */
  298. static void
  299. nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap)
  300. {
  301. /* sanitize the mode change */
  302. if (iap->ia_valid & ATTR_MODE) {
  303. iap->ia_mode &= S_IALLUGO;
  304. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  305. }
  306. /* Revoke setuid/setgid on chown */
  307. if (!S_ISDIR(inode->i_mode) &&
  308. ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
  309. iap->ia_valid |= ATTR_KILL_PRIV;
  310. if (iap->ia_valid & ATTR_MODE) {
  311. /* we're setting mode too, just clear the s*id bits */
  312. iap->ia_mode &= ~S_ISUID;
  313. if (iap->ia_mode & S_IXGRP)
  314. iap->ia_mode &= ~S_ISGID;
  315. } else {
  316. /* set ATTR_KILL_* bits and let VFS handle it */
  317. iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
  318. }
  319. }
  320. }
  321. static __be32
  322. nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp,
  323. struct iattr *iap)
  324. {
  325. struct inode *inode = d_inode(fhp->fh_dentry);
  326. int host_err;
  327. if (iap->ia_size < inode->i_size) {
  328. __be32 err;
  329. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  330. NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE);
  331. if (err)
  332. return err;
  333. }
  334. host_err = get_write_access(inode);
  335. if (host_err)
  336. goto out_nfserrno;
  337. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  338. if (host_err)
  339. goto out_put_write_access;
  340. return 0;
  341. out_put_write_access:
  342. put_write_access(inode);
  343. out_nfserrno:
  344. return nfserrno(host_err);
  345. }
  346. /*
  347. * Set various file attributes. After this call fhp needs an fh_put.
  348. */
  349. __be32
  350. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  351. int check_guard, time_t guardtime)
  352. {
  353. struct dentry *dentry;
  354. struct inode *inode;
  355. int accmode = NFSD_MAY_SATTR;
  356. umode_t ftype = 0;
  357. __be32 err;
  358. int host_err;
  359. bool get_write_count;
  360. bool size_change = (iap->ia_valid & ATTR_SIZE);
  361. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  362. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  363. if (iap->ia_valid & ATTR_SIZE)
  364. ftype = S_IFREG;
  365. /* Callers that do fh_verify should do the fh_want_write: */
  366. get_write_count = !fhp->fh_dentry;
  367. /* Get inode */
  368. err = fh_verify(rqstp, fhp, ftype, accmode);
  369. if (err)
  370. return err;
  371. if (get_write_count) {
  372. host_err = fh_want_write(fhp);
  373. if (host_err)
  374. goto out;
  375. }
  376. dentry = fhp->fh_dentry;
  377. inode = d_inode(dentry);
  378. /* Ignore any mode updates on symlinks */
  379. if (S_ISLNK(inode->i_mode))
  380. iap->ia_valid &= ~ATTR_MODE;
  381. if (!iap->ia_valid)
  382. return 0;
  383. nfsd_sanitize_attrs(inode, iap);
  384. if (check_guard && guardtime != inode->i_ctime.tv_sec)
  385. return nfserr_notsync;
  386. /*
  387. * The size case is special, it changes the file in addition to the
  388. * attributes, and file systems don't expect it to be mixed with
  389. * "random" attribute changes. We thus split out the size change
  390. * into a separate call to ->setattr, and do the rest as a separate
  391. * setattr call.
  392. */
  393. if (size_change) {
  394. err = nfsd_get_write_access(rqstp, fhp, iap);
  395. if (err)
  396. return err;
  397. }
  398. fh_lock(fhp);
  399. if (size_change) {
  400. /*
  401. * RFC5661, Section 18.30.4:
  402. * Changing the size of a file with SETATTR indirectly
  403. * changes the time_modify and change attributes.
  404. *
  405. * (and similar for the older RFCs)
  406. */
  407. struct iattr size_attr = {
  408. .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME,
  409. .ia_size = iap->ia_size,
  410. };
  411. host_err = notify_change(dentry, &size_attr, NULL);
  412. if (host_err)
  413. goto out_unlock;
  414. iap->ia_valid &= ~ATTR_SIZE;
  415. /*
  416. * Avoid the additional setattr call below if the only other
  417. * attribute that the client sends is the mtime, as we update
  418. * it as part of the size change above.
  419. */
  420. if ((iap->ia_valid & ~ATTR_MTIME) == 0)
  421. goto out_unlock;
  422. }
  423. iap->ia_valid |= ATTR_CTIME;
  424. host_err = notify_change(dentry, iap, NULL);
  425. out_unlock:
  426. fh_unlock(fhp);
  427. if (size_change)
  428. put_write_access(inode);
  429. out:
  430. if (!host_err)
  431. host_err = commit_metadata(fhp);
  432. return nfserrno(host_err);
  433. }
  434. #if defined(CONFIG_NFSD_V4)
  435. /*
  436. * NFS junction information is stored in an extended attribute.
  437. */
  438. #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs"
  439. /**
  440. * nfsd4_is_junction - Test if an object could be an NFS junction
  441. *
  442. * @dentry: object to test
  443. *
  444. * Returns 1 if "dentry" appears to contain NFS junction information.
  445. * Otherwise 0 is returned.
  446. */
  447. int nfsd4_is_junction(struct dentry *dentry)
  448. {
  449. struct inode *inode = d_inode(dentry);
  450. if (inode == NULL)
  451. return 0;
  452. if (inode->i_mode & S_IXUGO)
  453. return 0;
  454. if (!(inode->i_mode & S_ISVTX))
  455. return 0;
  456. if (vfs_getxattr(dentry, NFSD_JUNCTION_XATTR_NAME, NULL, 0) <= 0)
  457. return 0;
  458. return 1;
  459. }
  460. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  461. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  462. struct xdr_netobj *label)
  463. {
  464. __be32 error;
  465. int host_error;
  466. struct dentry *dentry;
  467. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
  468. if (error)
  469. return error;
  470. dentry = fhp->fh_dentry;
  471. inode_lock(d_inode(dentry));
  472. host_error = security_inode_setsecctx(dentry, label->data, label->len);
  473. inode_unlock(d_inode(dentry));
  474. return nfserrno(host_error);
  475. }
  476. #else
  477. __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp,
  478. struct xdr_netobj *label)
  479. {
  480. return nfserr_notsupp;
  481. }
  482. #endif
  483. __be32 nfsd4_clone_file_range(struct file *src, u64 src_pos, struct file *dst,
  484. u64 dst_pos, u64 count)
  485. {
  486. return nfserrno(do_clone_file_range(src, src_pos, dst, dst_pos, count));
  487. }
  488. ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
  489. u64 dst_pos, u64 count)
  490. {
  491. /*
  492. * Limit copy to 4MB to prevent indefinitely blocking an nfsd
  493. * thread and client rpc slot. The choice of 4MB is somewhat
  494. * arbitrary. We might instead base this on r/wsize, or make it
  495. * tunable, or use a time instead of a byte limit, or implement
  496. * asynchronous copy. In theory a client could also recognize a
  497. * limit like this and pipeline multiple COPY requests.
  498. */
  499. count = min_t(u64, count, 1 << 22);
  500. return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
  501. }
  502. __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp,
  503. struct file *file, loff_t offset, loff_t len,
  504. int flags)
  505. {
  506. int error;
  507. if (!S_ISREG(file_inode(file)->i_mode))
  508. return nfserr_inval;
  509. error = vfs_fallocate(file, flags, offset, len);
  510. if (!error)
  511. error = commit_metadata(fhp);
  512. return nfserrno(error);
  513. }
  514. #endif /* defined(CONFIG_NFSD_V4) */
  515. #ifdef CONFIG_NFSD_V3
  516. /*
  517. * Check server access rights to a file system object
  518. */
  519. struct accessmap {
  520. u32 access;
  521. int how;
  522. };
  523. static struct accessmap nfs3_regaccess[] = {
  524. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  525. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  526. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  527. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  528. { 0, 0 }
  529. };
  530. static struct accessmap nfs3_diraccess[] = {
  531. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  532. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  533. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  534. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  535. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  536. { 0, 0 }
  537. };
  538. static struct accessmap nfs3_anyaccess[] = {
  539. /* Some clients - Solaris 2.6 at least, make an access call
  540. * to the server to check for access for things like /dev/null
  541. * (which really, the server doesn't care about). So
  542. * We provide simple access checking for them, looking
  543. * mainly at mode bits, and we make sure to ignore read-only
  544. * filesystem checks
  545. */
  546. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  547. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  548. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  549. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  550. { 0, 0 }
  551. };
  552. __be32
  553. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  554. {
  555. struct accessmap *map;
  556. struct svc_export *export;
  557. struct dentry *dentry;
  558. u32 query, result = 0, sresult = 0;
  559. __be32 error;
  560. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  561. if (error)
  562. goto out;
  563. export = fhp->fh_export;
  564. dentry = fhp->fh_dentry;
  565. if (d_is_reg(dentry))
  566. map = nfs3_regaccess;
  567. else if (d_is_dir(dentry))
  568. map = nfs3_diraccess;
  569. else
  570. map = nfs3_anyaccess;
  571. query = *access;
  572. for (; map->access; map++) {
  573. if (map->access & query) {
  574. __be32 err2;
  575. sresult |= map->access;
  576. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  577. switch (err2) {
  578. case nfs_ok:
  579. result |= map->access;
  580. break;
  581. /* the following error codes just mean the access was not allowed,
  582. * rather than an error occurred */
  583. case nfserr_rofs:
  584. case nfserr_acces:
  585. case nfserr_perm:
  586. /* simply don't "or" in the access bit. */
  587. break;
  588. default:
  589. error = err2;
  590. goto out;
  591. }
  592. }
  593. }
  594. *access = result;
  595. if (supported)
  596. *supported = sresult;
  597. out:
  598. return error;
  599. }
  600. #endif /* CONFIG_NFSD_V3 */
  601. static int nfsd_open_break_lease(struct inode *inode, int access)
  602. {
  603. unsigned int mode;
  604. if (access & NFSD_MAY_NOT_BREAK_LEASE)
  605. return 0;
  606. mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY;
  607. return break_lease(inode, mode | O_NONBLOCK);
  608. }
  609. /*
  610. * Open an existing file or directory.
  611. * The may_flags argument indicates the type of open (read/write/lock)
  612. * and additional flags.
  613. * N.B. After this call fhp needs an fh_put
  614. */
  615. __be32
  616. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type,
  617. int may_flags, struct file **filp)
  618. {
  619. struct path path;
  620. struct inode *inode;
  621. struct file *file;
  622. int flags = O_RDONLY|O_LARGEFILE;
  623. __be32 err;
  624. int host_err = 0;
  625. validate_process_creds();
  626. /*
  627. * If we get here, then the client has already done an "open",
  628. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  629. * in case a chmod has now revoked permission.
  630. *
  631. * Arguably we should also allow the owner override for
  632. * directories, but we never have and it doesn't seem to have
  633. * caused anyone a problem. If we were to change this, note
  634. * also that our filldir callbacks would need a variant of
  635. * lookup_one_len that doesn't check permissions.
  636. */
  637. if (type == S_IFREG)
  638. may_flags |= NFSD_MAY_OWNER_OVERRIDE;
  639. err = fh_verify(rqstp, fhp, type, may_flags);
  640. if (err)
  641. goto out;
  642. path.mnt = fhp->fh_export->ex_path.mnt;
  643. path.dentry = fhp->fh_dentry;
  644. inode = d_inode(path.dentry);
  645. /* Disallow write access to files with the append-only bit set
  646. * or any access when mandatory locking enabled
  647. */
  648. err = nfserr_perm;
  649. if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE))
  650. goto out;
  651. /*
  652. * We must ignore files (but only files) which might have mandatory
  653. * locks on them because there is no way to know if the accesser has
  654. * the lock.
  655. */
  656. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  657. goto out;
  658. if (!inode->i_fop)
  659. goto out;
  660. host_err = nfsd_open_break_lease(inode, may_flags);
  661. if (host_err) /* NOMEM or WOULDBLOCK */
  662. goto out_nfserr;
  663. if (may_flags & NFSD_MAY_WRITE) {
  664. if (may_flags & NFSD_MAY_READ)
  665. flags = O_RDWR|O_LARGEFILE;
  666. else
  667. flags = O_WRONLY|O_LARGEFILE;
  668. }
  669. file = dentry_open(&path, flags, current_cred());
  670. if (IS_ERR(file)) {
  671. host_err = PTR_ERR(file);
  672. goto out_nfserr;
  673. }
  674. host_err = ima_file_check(file, may_flags, 0);
  675. if (host_err) {
  676. fput(file);
  677. goto out_nfserr;
  678. }
  679. if (may_flags & NFSD_MAY_64BIT_COOKIE)
  680. file->f_mode |= FMODE_64BITHASH;
  681. else
  682. file->f_mode |= FMODE_32BITHASH;
  683. *filp = file;
  684. out_nfserr:
  685. err = nfserrno(host_err);
  686. out:
  687. validate_process_creds();
  688. return err;
  689. }
  690. struct raparms *
  691. nfsd_init_raparms(struct file *file)
  692. {
  693. struct inode *inode = file_inode(file);
  694. dev_t dev = inode->i_sb->s_dev;
  695. ino_t ino = inode->i_ino;
  696. struct raparms *ra, **rap, **frap = NULL;
  697. int depth = 0;
  698. unsigned int hash;
  699. struct raparm_hbucket *rab;
  700. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  701. rab = &raparm_hash[hash];
  702. spin_lock(&rab->pb_lock);
  703. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  704. if (ra->p_ino == ino && ra->p_dev == dev)
  705. goto found;
  706. depth++;
  707. if (ra->p_count == 0)
  708. frap = rap;
  709. }
  710. depth = nfsdstats.ra_size;
  711. if (!frap) {
  712. spin_unlock(&rab->pb_lock);
  713. return NULL;
  714. }
  715. rap = frap;
  716. ra = *frap;
  717. ra->p_dev = dev;
  718. ra->p_ino = ino;
  719. ra->p_set = 0;
  720. ra->p_hindex = hash;
  721. found:
  722. if (rap != &rab->pb_head) {
  723. *rap = ra->p_next;
  724. ra->p_next = rab->pb_head;
  725. rab->pb_head = ra;
  726. }
  727. ra->p_count++;
  728. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  729. spin_unlock(&rab->pb_lock);
  730. if (ra->p_set)
  731. file->f_ra = ra->p_ra;
  732. return ra;
  733. }
  734. void nfsd_put_raparams(struct file *file, struct raparms *ra)
  735. {
  736. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  737. spin_lock(&rab->pb_lock);
  738. ra->p_ra = file->f_ra;
  739. ra->p_set = 1;
  740. ra->p_count--;
  741. spin_unlock(&rab->pb_lock);
  742. }
  743. /*
  744. * Grab and keep cached pages associated with a file in the svc_rqst
  745. * so that they can be passed to the network sendmsg/sendpage routines
  746. * directly. They will be released after the sending has completed.
  747. */
  748. static int
  749. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  750. struct splice_desc *sd)
  751. {
  752. struct svc_rqst *rqstp = sd->u.data;
  753. struct page **pp = rqstp->rq_next_page;
  754. struct page *page = buf->page;
  755. size_t size;
  756. size = sd->len;
  757. if (rqstp->rq_res.page_len == 0) {
  758. get_page(page);
  759. put_page(*rqstp->rq_next_page);
  760. *(rqstp->rq_next_page++) = page;
  761. rqstp->rq_res.page_base = buf->offset;
  762. rqstp->rq_res.page_len = size;
  763. } else if (page != pp[-1]) {
  764. get_page(page);
  765. if (*rqstp->rq_next_page)
  766. put_page(*rqstp->rq_next_page);
  767. *(rqstp->rq_next_page++) = page;
  768. rqstp->rq_res.page_len += size;
  769. } else
  770. rqstp->rq_res.page_len += size;
  771. return size;
  772. }
  773. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  774. struct splice_desc *sd)
  775. {
  776. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  777. }
  778. static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  779. struct file *file, loff_t offset,
  780. unsigned long *count, int host_err)
  781. {
  782. if (host_err >= 0) {
  783. nfsdstats.io_read += host_err;
  784. *count = host_err;
  785. fsnotify_access(file);
  786. trace_nfsd_read_io_done(rqstp, fhp, offset, *count);
  787. return 0;
  788. } else {
  789. trace_nfsd_read_err(rqstp, fhp, offset, host_err);
  790. return nfserrno(host_err);
  791. }
  792. }
  793. __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  794. struct file *file, loff_t offset, unsigned long *count)
  795. {
  796. struct splice_desc sd = {
  797. .len = 0,
  798. .total_len = *count,
  799. .pos = offset,
  800. .u.data = rqstp,
  801. };
  802. int host_err;
  803. trace_nfsd_read_splice(rqstp, fhp, offset, *count);
  804. rqstp->rq_next_page = rqstp->rq_respages + 1;
  805. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  806. return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
  807. }
  808. __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp,
  809. struct file *file, loff_t offset,
  810. struct kvec *vec, int vlen, unsigned long *count)
  811. {
  812. struct iov_iter iter;
  813. int host_err;
  814. trace_nfsd_read_vector(rqstp, fhp, offset, *count);
  815. iov_iter_kvec(&iter, READ | ITER_KVEC, vec, vlen, *count);
  816. host_err = vfs_iter_read(file, &iter, &offset, 0);
  817. return nfsd_finish_read(rqstp, fhp, file, offset, count, host_err);
  818. }
  819. /*
  820. * Gathered writes: If another process is currently writing to the file,
  821. * there's a high chance this is another nfsd (triggered by a bulk write
  822. * from a client's biod). Rather than syncing the file with each write
  823. * request, we sleep for 10 msec.
  824. *
  825. * I don't know if this roughly approximates C. Juszak's idea of
  826. * gathered writes, but it's a nice and simple solution (IMHO), and it
  827. * seems to work:-)
  828. *
  829. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  830. * better tool (separate unstable writes and commits) for solving this
  831. * problem.
  832. */
  833. static int wait_for_concurrent_writes(struct file *file)
  834. {
  835. struct inode *inode = file_inode(file);
  836. static ino_t last_ino;
  837. static dev_t last_dev;
  838. int err = 0;
  839. if (atomic_read(&inode->i_writecount) > 1
  840. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  841. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  842. msleep(10);
  843. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  844. }
  845. if (inode->i_state & I_DIRTY) {
  846. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  847. err = vfs_fsync(file, 0);
  848. }
  849. last_ino = inode->i_ino;
  850. last_dev = inode->i_sb->s_dev;
  851. return err;
  852. }
  853. __be32
  854. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  855. loff_t offset, struct kvec *vec, int vlen,
  856. unsigned long *cnt, int stable)
  857. {
  858. struct svc_export *exp;
  859. struct iov_iter iter;
  860. __be32 nfserr;
  861. int host_err;
  862. int use_wgather;
  863. loff_t pos = offset;
  864. unsigned int pflags = current->flags;
  865. rwf_t flags = 0;
  866. trace_nfsd_write_opened(rqstp, fhp, offset, *cnt);
  867. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  868. /*
  869. * We want less throttling in balance_dirty_pages()
  870. * and shrink_inactive_list() so that nfs to
  871. * localhost doesn't cause nfsd to lock up due to all
  872. * the client's dirty pages or its congested queue.
  873. */
  874. current->flags |= PF_LESS_THROTTLE;
  875. exp = fhp->fh_export;
  876. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  877. if (!EX_ISSYNC(exp))
  878. stable = NFS_UNSTABLE;
  879. if (stable && !use_wgather)
  880. flags |= RWF_SYNC;
  881. iov_iter_kvec(&iter, WRITE | ITER_KVEC, vec, vlen, *cnt);
  882. host_err = vfs_iter_write(file, &iter, &pos, flags);
  883. if (host_err < 0)
  884. goto out_nfserr;
  885. nfsdstats.io_write += *cnt;
  886. fsnotify_modify(file);
  887. if (stable && use_wgather)
  888. host_err = wait_for_concurrent_writes(file);
  889. out_nfserr:
  890. if (host_err >= 0) {
  891. trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt);
  892. nfserr = nfs_ok;
  893. } else {
  894. trace_nfsd_write_err(rqstp, fhp, offset, host_err);
  895. nfserr = nfserrno(host_err);
  896. }
  897. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  898. current_restore_flags(pflags, PF_LESS_THROTTLE);
  899. return nfserr;
  900. }
  901. /*
  902. * Read data from a file. count must contain the requested read count
  903. * on entry. On return, *count contains the number of bytes actually read.
  904. * N.B. After this call fhp needs an fh_put
  905. */
  906. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  907. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  908. {
  909. struct file *file;
  910. struct raparms *ra;
  911. __be32 err;
  912. trace_nfsd_read_start(rqstp, fhp, offset, *count);
  913. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  914. if (err)
  915. return err;
  916. ra = nfsd_init_raparms(file);
  917. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
  918. err = nfsd_splice_read(rqstp, fhp, file, offset, count);
  919. else
  920. err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count);
  921. if (ra)
  922. nfsd_put_raparams(file, ra);
  923. fput(file);
  924. trace_nfsd_read_done(rqstp, fhp, offset, *count);
  925. return err;
  926. }
  927. /*
  928. * Write data to a file.
  929. * The stable flag requests synchronous writes.
  930. * N.B. After this call fhp needs an fh_put
  931. */
  932. __be32
  933. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset,
  934. struct kvec *vec, int vlen, unsigned long *cnt, int stable)
  935. {
  936. struct file *file = NULL;
  937. __be32 err = 0;
  938. trace_nfsd_write_start(rqstp, fhp, offset, *cnt);
  939. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  940. if (err)
  941. goto out;
  942. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt, stable);
  943. fput(file);
  944. out:
  945. trace_nfsd_write_done(rqstp, fhp, offset, *cnt);
  946. return err;
  947. }
  948. #ifdef CONFIG_NFSD_V3
  949. /*
  950. * Commit all pending writes to stable storage.
  951. *
  952. * Note: we only guarantee that data that lies within the range specified
  953. * by the 'offset' and 'count' parameters will be synced.
  954. *
  955. * Unfortunately we cannot lock the file to make sure we return full WCC
  956. * data to the client, as locking happens lower down in the filesystem.
  957. */
  958. __be32
  959. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  960. loff_t offset, unsigned long count)
  961. {
  962. struct file *file;
  963. loff_t end = LLONG_MAX;
  964. __be32 err = nfserr_inval;
  965. if (offset < 0)
  966. goto out;
  967. if (count != 0) {
  968. end = offset + (loff_t)count - 1;
  969. if (end < offset)
  970. goto out;
  971. }
  972. err = nfsd_open(rqstp, fhp, S_IFREG,
  973. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  974. if (err)
  975. goto out;
  976. if (EX_ISSYNC(fhp->fh_export)) {
  977. int err2 = vfs_fsync_range(file, offset, end, 0);
  978. if (err2 != -EINVAL)
  979. err = nfserrno(err2);
  980. else
  981. err = nfserr_notsupp;
  982. }
  983. fput(file);
  984. out:
  985. return err;
  986. }
  987. #endif /* CONFIG_NFSD_V3 */
  988. static __be32
  989. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  990. struct iattr *iap)
  991. {
  992. /*
  993. * Mode has already been set earlier in create:
  994. */
  995. iap->ia_valid &= ~ATTR_MODE;
  996. /*
  997. * Setting uid/gid works only for root. Irix appears to
  998. * send along the gid on create when it tries to implement
  999. * setgid directories via NFS:
  1000. */
  1001. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  1002. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1003. if (iap->ia_valid)
  1004. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1005. /* Callers expect file metadata to be committed here */
  1006. return nfserrno(commit_metadata(resfhp));
  1007. }
  1008. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1009. * setting size to 0 may fail for some specific file systems by the permission
  1010. * checking which requires WRITE permission but the mode is 000.
  1011. * we ignore the resizing(to 0) on the just new created file, since the size is
  1012. * 0 after file created.
  1013. *
  1014. * call this only after vfs_create() is called.
  1015. * */
  1016. static void
  1017. nfsd_check_ignore_resizing(struct iattr *iap)
  1018. {
  1019. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1020. iap->ia_valid &= ~ATTR_SIZE;
  1021. }
  1022. /* The parent directory should already be locked: */
  1023. __be32
  1024. nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1025. char *fname, int flen, struct iattr *iap,
  1026. int type, dev_t rdev, struct svc_fh *resfhp)
  1027. {
  1028. struct dentry *dentry, *dchild;
  1029. struct inode *dirp;
  1030. __be32 err;
  1031. __be32 err2;
  1032. int host_err;
  1033. dentry = fhp->fh_dentry;
  1034. dirp = d_inode(dentry);
  1035. dchild = dget(resfhp->fh_dentry);
  1036. if (!fhp->fh_locked) {
  1037. WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n",
  1038. dentry);
  1039. err = nfserr_io;
  1040. goto out;
  1041. }
  1042. err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE);
  1043. if (err)
  1044. goto out;
  1045. if (!(iap->ia_valid & ATTR_MODE))
  1046. iap->ia_mode = 0;
  1047. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1048. err = 0;
  1049. host_err = 0;
  1050. switch (type) {
  1051. case S_IFREG:
  1052. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1053. if (!host_err)
  1054. nfsd_check_ignore_resizing(iap);
  1055. break;
  1056. case S_IFDIR:
  1057. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1058. if (!host_err && unlikely(d_unhashed(dchild))) {
  1059. struct dentry *d;
  1060. d = lookup_one_len(dchild->d_name.name,
  1061. dchild->d_parent,
  1062. dchild->d_name.len);
  1063. if (IS_ERR(d)) {
  1064. host_err = PTR_ERR(d);
  1065. break;
  1066. }
  1067. if (unlikely(d_is_negative(d))) {
  1068. dput(d);
  1069. err = nfserr_serverfault;
  1070. goto out;
  1071. }
  1072. dput(resfhp->fh_dentry);
  1073. resfhp->fh_dentry = dget(d);
  1074. err = fh_update(resfhp);
  1075. dput(dchild);
  1076. dchild = d;
  1077. if (err)
  1078. goto out;
  1079. }
  1080. break;
  1081. case S_IFCHR:
  1082. case S_IFBLK:
  1083. case S_IFIFO:
  1084. case S_IFSOCK:
  1085. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1086. break;
  1087. default:
  1088. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1089. type);
  1090. host_err = -EINVAL;
  1091. }
  1092. if (host_err < 0)
  1093. goto out_nfserr;
  1094. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1095. /*
  1096. * nfsd_create_setattr already committed the child. Transactional
  1097. * filesystems had a chance to commit changes for both parent and
  1098. * child simultaneously making the following commit_metadata a
  1099. * noop.
  1100. */
  1101. err2 = nfserrno(commit_metadata(fhp));
  1102. if (err2)
  1103. err = err2;
  1104. /*
  1105. * Update the file handle to get the new inode info.
  1106. */
  1107. if (!err)
  1108. err = fh_update(resfhp);
  1109. out:
  1110. dput(dchild);
  1111. return err;
  1112. out_nfserr:
  1113. err = nfserrno(host_err);
  1114. goto out;
  1115. }
  1116. /*
  1117. * Create a filesystem object (regular, directory, special).
  1118. * Note that the parent directory is left locked.
  1119. *
  1120. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1121. */
  1122. __be32
  1123. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1124. char *fname, int flen, struct iattr *iap,
  1125. int type, dev_t rdev, struct svc_fh *resfhp)
  1126. {
  1127. struct dentry *dentry, *dchild = NULL;
  1128. struct inode *dirp;
  1129. __be32 err;
  1130. int host_err;
  1131. if (isdotent(fname, flen))
  1132. return nfserr_exist;
  1133. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP);
  1134. if (err)
  1135. return err;
  1136. dentry = fhp->fh_dentry;
  1137. dirp = d_inode(dentry);
  1138. host_err = fh_want_write(fhp);
  1139. if (host_err)
  1140. return nfserrno(host_err);
  1141. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1142. dchild = lookup_one_len(fname, dentry, flen);
  1143. host_err = PTR_ERR(dchild);
  1144. if (IS_ERR(dchild))
  1145. return nfserrno(host_err);
  1146. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1147. /*
  1148. * We unconditionally drop our ref to dchild as fh_compose will have
  1149. * already grabbed its own ref for it.
  1150. */
  1151. dput(dchild);
  1152. if (err)
  1153. return err;
  1154. return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type,
  1155. rdev, resfhp);
  1156. }
  1157. #ifdef CONFIG_NFSD_V3
  1158. /*
  1159. * NFSv3 and NFSv4 version of nfsd_create
  1160. */
  1161. __be32
  1162. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1163. char *fname, int flen, struct iattr *iap,
  1164. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1165. bool *truncp, bool *created)
  1166. {
  1167. struct dentry *dentry, *dchild = NULL;
  1168. struct inode *dirp;
  1169. __be32 err;
  1170. int host_err;
  1171. __u32 v_mtime=0, v_atime=0;
  1172. err = nfserr_perm;
  1173. if (!flen)
  1174. goto out;
  1175. err = nfserr_exist;
  1176. if (isdotent(fname, flen))
  1177. goto out;
  1178. if (!(iap->ia_valid & ATTR_MODE))
  1179. iap->ia_mode = 0;
  1180. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1181. if (err)
  1182. goto out;
  1183. dentry = fhp->fh_dentry;
  1184. dirp = d_inode(dentry);
  1185. host_err = fh_want_write(fhp);
  1186. if (host_err)
  1187. goto out_nfserr;
  1188. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1189. /*
  1190. * Compose the response file handle.
  1191. */
  1192. dchild = lookup_one_len(fname, dentry, flen);
  1193. host_err = PTR_ERR(dchild);
  1194. if (IS_ERR(dchild))
  1195. goto out_nfserr;
  1196. /* If file doesn't exist, check for permissions to create one */
  1197. if (d_really_is_negative(dchild)) {
  1198. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1199. if (err)
  1200. goto out;
  1201. }
  1202. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1203. if (err)
  1204. goto out;
  1205. if (nfsd_create_is_exclusive(createmode)) {
  1206. /* solaris7 gets confused (bugid 4218508) if these have
  1207. * the high bit set, so just clear the high bits. If this is
  1208. * ever changed to use different attrs for storing the
  1209. * verifier, then do_open_lookup() will also need to be fixed
  1210. * accordingly.
  1211. */
  1212. v_mtime = verifier[0]&0x7fffffff;
  1213. v_atime = verifier[1]&0x7fffffff;
  1214. }
  1215. if (d_really_is_positive(dchild)) {
  1216. err = 0;
  1217. switch (createmode) {
  1218. case NFS3_CREATE_UNCHECKED:
  1219. if (! d_is_reg(dchild))
  1220. goto out;
  1221. else if (truncp) {
  1222. /* in nfsv4, we need to treat this case a little
  1223. * differently. we don't want to truncate the
  1224. * file now; this would be wrong if the OPEN
  1225. * fails for some other reason. furthermore,
  1226. * if the size is nonzero, we should ignore it
  1227. * according to spec!
  1228. */
  1229. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1230. }
  1231. else {
  1232. iap->ia_valid &= ATTR_SIZE;
  1233. goto set_attr;
  1234. }
  1235. break;
  1236. case NFS3_CREATE_EXCLUSIVE:
  1237. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1238. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1239. && d_inode(dchild)->i_size == 0 ) {
  1240. if (created)
  1241. *created = 1;
  1242. break;
  1243. }
  1244. case NFS4_CREATE_EXCLUSIVE4_1:
  1245. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1246. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1247. && d_inode(dchild)->i_size == 0 ) {
  1248. if (created)
  1249. *created = 1;
  1250. goto set_attr;
  1251. }
  1252. /* fallthru */
  1253. case NFS3_CREATE_GUARDED:
  1254. err = nfserr_exist;
  1255. }
  1256. fh_drop_write(fhp);
  1257. goto out;
  1258. }
  1259. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1260. if (host_err < 0) {
  1261. fh_drop_write(fhp);
  1262. goto out_nfserr;
  1263. }
  1264. if (created)
  1265. *created = 1;
  1266. nfsd_check_ignore_resizing(iap);
  1267. if (nfsd_create_is_exclusive(createmode)) {
  1268. /* Cram the verifier into atime/mtime */
  1269. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1270. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1271. /* XXX someone who knows this better please fix it for nsec */
  1272. iap->ia_mtime.tv_sec = v_mtime;
  1273. iap->ia_atime.tv_sec = v_atime;
  1274. iap->ia_mtime.tv_nsec = 0;
  1275. iap->ia_atime.tv_nsec = 0;
  1276. }
  1277. set_attr:
  1278. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1279. /*
  1280. * nfsd_create_setattr already committed the child
  1281. * (and possibly also the parent).
  1282. */
  1283. if (!err)
  1284. err = nfserrno(commit_metadata(fhp));
  1285. /*
  1286. * Update the filehandle to get the new inode info.
  1287. */
  1288. if (!err)
  1289. err = fh_update(resfhp);
  1290. out:
  1291. fh_unlock(fhp);
  1292. if (dchild && !IS_ERR(dchild))
  1293. dput(dchild);
  1294. fh_drop_write(fhp);
  1295. return err;
  1296. out_nfserr:
  1297. err = nfserrno(host_err);
  1298. goto out;
  1299. }
  1300. #endif /* CONFIG_NFSD_V3 */
  1301. /*
  1302. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1303. * fits into the buffer. On return, it contains the true length.
  1304. * N.B. After this call fhp needs an fh_put
  1305. */
  1306. __be32
  1307. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1308. {
  1309. __be32 err;
  1310. const char *link;
  1311. struct path path;
  1312. DEFINE_DELAYED_CALL(done);
  1313. int len;
  1314. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1315. if (unlikely(err))
  1316. return err;
  1317. path.mnt = fhp->fh_export->ex_path.mnt;
  1318. path.dentry = fhp->fh_dentry;
  1319. if (unlikely(!d_is_symlink(path.dentry)))
  1320. return nfserr_inval;
  1321. touch_atime(&path);
  1322. link = vfs_get_link(path.dentry, &done);
  1323. if (IS_ERR(link))
  1324. return nfserrno(PTR_ERR(link));
  1325. len = strlen(link);
  1326. if (len < *lenp)
  1327. *lenp = len;
  1328. memcpy(buf, link, *lenp);
  1329. do_delayed_call(&done);
  1330. return 0;
  1331. }
  1332. /*
  1333. * Create a symlink and look up its inode
  1334. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1335. */
  1336. __be32
  1337. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1338. char *fname, int flen,
  1339. char *path,
  1340. struct svc_fh *resfhp)
  1341. {
  1342. struct dentry *dentry, *dnew;
  1343. __be32 err, cerr;
  1344. int host_err;
  1345. err = nfserr_noent;
  1346. if (!flen || path[0] == '\0')
  1347. goto out;
  1348. err = nfserr_exist;
  1349. if (isdotent(fname, flen))
  1350. goto out;
  1351. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1352. if (err)
  1353. goto out;
  1354. host_err = fh_want_write(fhp);
  1355. if (host_err)
  1356. goto out_nfserr;
  1357. fh_lock(fhp);
  1358. dentry = fhp->fh_dentry;
  1359. dnew = lookup_one_len(fname, dentry, flen);
  1360. host_err = PTR_ERR(dnew);
  1361. if (IS_ERR(dnew))
  1362. goto out_nfserr;
  1363. host_err = vfs_symlink(d_inode(dentry), dnew, path);
  1364. err = nfserrno(host_err);
  1365. if (!err)
  1366. err = nfserrno(commit_metadata(fhp));
  1367. fh_unlock(fhp);
  1368. fh_drop_write(fhp);
  1369. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1370. dput(dnew);
  1371. if (err==0) err = cerr;
  1372. out:
  1373. return err;
  1374. out_nfserr:
  1375. err = nfserrno(host_err);
  1376. goto out;
  1377. }
  1378. /*
  1379. * Create a hardlink
  1380. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1381. */
  1382. __be32
  1383. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1384. char *name, int len, struct svc_fh *tfhp)
  1385. {
  1386. struct dentry *ddir, *dnew, *dold;
  1387. struct inode *dirp;
  1388. __be32 err;
  1389. int host_err;
  1390. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1391. if (err)
  1392. goto out;
  1393. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1394. if (err)
  1395. goto out;
  1396. err = nfserr_isdir;
  1397. if (d_is_dir(tfhp->fh_dentry))
  1398. goto out;
  1399. err = nfserr_perm;
  1400. if (!len)
  1401. goto out;
  1402. err = nfserr_exist;
  1403. if (isdotent(name, len))
  1404. goto out;
  1405. host_err = fh_want_write(tfhp);
  1406. if (host_err) {
  1407. err = nfserrno(host_err);
  1408. goto out;
  1409. }
  1410. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1411. ddir = ffhp->fh_dentry;
  1412. dirp = d_inode(ddir);
  1413. dnew = lookup_one_len(name, ddir, len);
  1414. host_err = PTR_ERR(dnew);
  1415. if (IS_ERR(dnew))
  1416. goto out_nfserr;
  1417. dold = tfhp->fh_dentry;
  1418. err = nfserr_noent;
  1419. if (d_really_is_negative(dold))
  1420. goto out_dput;
  1421. host_err = vfs_link(dold, dirp, dnew, NULL);
  1422. if (!host_err) {
  1423. err = nfserrno(commit_metadata(ffhp));
  1424. if (!err)
  1425. err = nfserrno(commit_metadata(tfhp));
  1426. } else {
  1427. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1428. err = nfserr_acces;
  1429. else
  1430. err = nfserrno(host_err);
  1431. }
  1432. out_dput:
  1433. dput(dnew);
  1434. out_unlock:
  1435. fh_unlock(ffhp);
  1436. fh_drop_write(tfhp);
  1437. out:
  1438. return err;
  1439. out_nfserr:
  1440. err = nfserrno(host_err);
  1441. goto out_unlock;
  1442. }
  1443. /*
  1444. * Rename a file
  1445. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1446. */
  1447. __be32
  1448. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1449. struct svc_fh *tfhp, char *tname, int tlen)
  1450. {
  1451. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1452. struct inode *fdir, *tdir;
  1453. __be32 err;
  1454. int host_err;
  1455. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1456. if (err)
  1457. goto out;
  1458. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1459. if (err)
  1460. goto out;
  1461. fdentry = ffhp->fh_dentry;
  1462. fdir = d_inode(fdentry);
  1463. tdentry = tfhp->fh_dentry;
  1464. tdir = d_inode(tdentry);
  1465. err = nfserr_perm;
  1466. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1467. goto out;
  1468. host_err = fh_want_write(ffhp);
  1469. if (host_err) {
  1470. err = nfserrno(host_err);
  1471. goto out;
  1472. }
  1473. /* cannot use fh_lock as we need deadlock protective ordering
  1474. * so do it by hand */
  1475. trap = lock_rename(tdentry, fdentry);
  1476. ffhp->fh_locked = tfhp->fh_locked = true;
  1477. fill_pre_wcc(ffhp);
  1478. fill_pre_wcc(tfhp);
  1479. odentry = lookup_one_len(fname, fdentry, flen);
  1480. host_err = PTR_ERR(odentry);
  1481. if (IS_ERR(odentry))
  1482. goto out_nfserr;
  1483. host_err = -ENOENT;
  1484. if (d_really_is_negative(odentry))
  1485. goto out_dput_old;
  1486. host_err = -EINVAL;
  1487. if (odentry == trap)
  1488. goto out_dput_old;
  1489. ndentry = lookup_one_len(tname, tdentry, tlen);
  1490. host_err = PTR_ERR(ndentry);
  1491. if (IS_ERR(ndentry))
  1492. goto out_dput_old;
  1493. host_err = -ENOTEMPTY;
  1494. if (ndentry == trap)
  1495. goto out_dput_new;
  1496. host_err = -EXDEV;
  1497. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1498. goto out_dput_new;
  1499. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1500. goto out_dput_new;
  1501. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1502. if (!host_err) {
  1503. host_err = commit_metadata(tfhp);
  1504. if (!host_err)
  1505. host_err = commit_metadata(ffhp);
  1506. }
  1507. out_dput_new:
  1508. dput(ndentry);
  1509. out_dput_old:
  1510. dput(odentry);
  1511. out_nfserr:
  1512. err = nfserrno(host_err);
  1513. /*
  1514. * We cannot rely on fh_unlock on the two filehandles,
  1515. * as that would do the wrong thing if the two directories
  1516. * were the same, so again we do it by hand.
  1517. */
  1518. fill_post_wcc(ffhp);
  1519. fill_post_wcc(tfhp);
  1520. unlock_rename(tdentry, fdentry);
  1521. ffhp->fh_locked = tfhp->fh_locked = false;
  1522. fh_drop_write(ffhp);
  1523. out:
  1524. return err;
  1525. }
  1526. /*
  1527. * Unlink a file or directory
  1528. * N.B. After this call fhp needs an fh_put
  1529. */
  1530. __be32
  1531. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1532. char *fname, int flen)
  1533. {
  1534. struct dentry *dentry, *rdentry;
  1535. struct inode *dirp;
  1536. __be32 err;
  1537. int host_err;
  1538. err = nfserr_acces;
  1539. if (!flen || isdotent(fname, flen))
  1540. goto out;
  1541. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1542. if (err)
  1543. goto out;
  1544. host_err = fh_want_write(fhp);
  1545. if (host_err)
  1546. goto out_nfserr;
  1547. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1548. dentry = fhp->fh_dentry;
  1549. dirp = d_inode(dentry);
  1550. rdentry = lookup_one_len(fname, dentry, flen);
  1551. host_err = PTR_ERR(rdentry);
  1552. if (IS_ERR(rdentry))
  1553. goto out_nfserr;
  1554. if (d_really_is_negative(rdentry)) {
  1555. dput(rdentry);
  1556. err = nfserr_noent;
  1557. goto out;
  1558. }
  1559. if (!type)
  1560. type = d_inode(rdentry)->i_mode & S_IFMT;
  1561. if (type != S_IFDIR)
  1562. host_err = vfs_unlink(dirp, rdentry, NULL);
  1563. else
  1564. host_err = vfs_rmdir(dirp, rdentry);
  1565. if (!host_err)
  1566. host_err = commit_metadata(fhp);
  1567. dput(rdentry);
  1568. out_nfserr:
  1569. err = nfserrno(host_err);
  1570. out:
  1571. return err;
  1572. }
  1573. /*
  1574. * We do this buffering because we must not call back into the file
  1575. * system's ->lookup() method from the filldir callback. That may well
  1576. * deadlock a number of file systems.
  1577. *
  1578. * This is based heavily on the implementation of same in XFS.
  1579. */
  1580. struct buffered_dirent {
  1581. u64 ino;
  1582. loff_t offset;
  1583. int namlen;
  1584. unsigned int d_type;
  1585. char name[];
  1586. };
  1587. struct readdir_data {
  1588. struct dir_context ctx;
  1589. char *dirent;
  1590. size_t used;
  1591. int full;
  1592. };
  1593. static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1594. int namlen, loff_t offset, u64 ino,
  1595. unsigned int d_type)
  1596. {
  1597. struct readdir_data *buf =
  1598. container_of(ctx, struct readdir_data, ctx);
  1599. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1600. unsigned int reclen;
  1601. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1602. if (buf->used + reclen > PAGE_SIZE) {
  1603. buf->full = 1;
  1604. return -EINVAL;
  1605. }
  1606. de->namlen = namlen;
  1607. de->offset = offset;
  1608. de->ino = ino;
  1609. de->d_type = d_type;
  1610. memcpy(de->name, name, namlen);
  1611. buf->used += reclen;
  1612. return 0;
  1613. }
  1614. static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
  1615. struct readdir_cd *cdp, loff_t *offsetp)
  1616. {
  1617. struct buffered_dirent *de;
  1618. int host_err;
  1619. int size;
  1620. loff_t offset;
  1621. struct readdir_data buf = {
  1622. .ctx.actor = nfsd_buffered_filldir,
  1623. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1624. };
  1625. if (!buf.dirent)
  1626. return nfserrno(-ENOMEM);
  1627. offset = *offsetp;
  1628. while (1) {
  1629. unsigned int reclen;
  1630. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1631. buf.used = 0;
  1632. buf.full = 0;
  1633. host_err = iterate_dir(file, &buf.ctx);
  1634. if (buf.full)
  1635. host_err = 0;
  1636. if (host_err < 0)
  1637. break;
  1638. size = buf.used;
  1639. if (!size)
  1640. break;
  1641. de = (struct buffered_dirent *)buf.dirent;
  1642. while (size > 0) {
  1643. offset = de->offset;
  1644. if (func(cdp, de->name, de->namlen, de->offset,
  1645. de->ino, de->d_type))
  1646. break;
  1647. if (cdp->err != nfs_ok)
  1648. break;
  1649. reclen = ALIGN(sizeof(*de) + de->namlen,
  1650. sizeof(u64));
  1651. size -= reclen;
  1652. de = (struct buffered_dirent *)((char *)de + reclen);
  1653. }
  1654. if (size > 0) /* We bailed out early */
  1655. break;
  1656. offset = vfs_llseek(file, 0, SEEK_CUR);
  1657. }
  1658. free_page((unsigned long)(buf.dirent));
  1659. if (host_err)
  1660. return nfserrno(host_err);
  1661. *offsetp = offset;
  1662. return cdp->err;
  1663. }
  1664. /*
  1665. * Read entries from a directory.
  1666. * The NFSv3/4 verifier we ignore for now.
  1667. */
  1668. __be32
  1669. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1670. struct readdir_cd *cdp, nfsd_filldir_t func)
  1671. {
  1672. __be32 err;
  1673. struct file *file;
  1674. loff_t offset = *offsetp;
  1675. int may_flags = NFSD_MAY_READ;
  1676. /* NFSv2 only supports 32 bit cookies */
  1677. if (rqstp->rq_vers > 2)
  1678. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1679. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1680. if (err)
  1681. goto out;
  1682. offset = vfs_llseek(file, offset, SEEK_SET);
  1683. if (offset < 0) {
  1684. err = nfserrno((int)offset);
  1685. goto out_close;
  1686. }
  1687. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1688. if (err == nfserr_eof || err == nfserr_toosmall)
  1689. err = nfs_ok; /* can still be found in ->err */
  1690. out_close:
  1691. fput(file);
  1692. out:
  1693. return err;
  1694. }
  1695. /*
  1696. * Get file system stats
  1697. * N.B. After this call fhp needs an fh_put
  1698. */
  1699. __be32
  1700. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1701. {
  1702. __be32 err;
  1703. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1704. if (!err) {
  1705. struct path path = {
  1706. .mnt = fhp->fh_export->ex_path.mnt,
  1707. .dentry = fhp->fh_dentry,
  1708. };
  1709. if (vfs_statfs(&path, stat))
  1710. err = nfserr_io;
  1711. }
  1712. return err;
  1713. }
  1714. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1715. {
  1716. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1717. }
  1718. /*
  1719. * Check for a user's access permissions to this inode.
  1720. */
  1721. __be32
  1722. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1723. struct dentry *dentry, int acc)
  1724. {
  1725. struct inode *inode = d_inode(dentry);
  1726. int err;
  1727. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1728. return 0;
  1729. #if 0
  1730. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1731. acc,
  1732. (acc & NFSD_MAY_READ)? " read" : "",
  1733. (acc & NFSD_MAY_WRITE)? " write" : "",
  1734. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1735. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1736. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1737. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1738. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1739. inode->i_mode,
  1740. IS_IMMUTABLE(inode)? " immut" : "",
  1741. IS_APPEND(inode)? " append" : "",
  1742. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1743. dprintk(" owner %d/%d user %d/%d\n",
  1744. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1745. #endif
  1746. /* Normally we reject any write/sattr etc access on a read-only file
  1747. * system. But if it is IRIX doing check on write-access for a
  1748. * device special file, we ignore rofs.
  1749. */
  1750. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1751. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1752. if (exp_rdonly(rqstp, exp) ||
  1753. __mnt_is_readonly(exp->ex_path.mnt))
  1754. return nfserr_rofs;
  1755. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1756. return nfserr_perm;
  1757. }
  1758. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1759. return nfserr_perm;
  1760. if (acc & NFSD_MAY_LOCK) {
  1761. /* If we cannot rely on authentication in NLM requests,
  1762. * just allow locks, otherwise require read permission, or
  1763. * ownership
  1764. */
  1765. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1766. return 0;
  1767. else
  1768. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1769. }
  1770. /*
  1771. * The file owner always gets access permission for accesses that
  1772. * would normally be checked at open time. This is to make
  1773. * file access work even when the client has done a fchmod(fd, 0).
  1774. *
  1775. * However, `cp foo bar' should fail nevertheless when bar is
  1776. * readonly. A sensible way to do this might be to reject all
  1777. * attempts to truncate a read-only file, because a creat() call
  1778. * always implies file truncation.
  1779. * ... but this isn't really fair. A process may reasonably call
  1780. * ftruncate on an open file descriptor on a file with perm 000.
  1781. * We must trust the client to do permission checking - using "ACCESS"
  1782. * with NFSv3.
  1783. */
  1784. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1785. uid_eq(inode->i_uid, current_fsuid()))
  1786. return 0;
  1787. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1788. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1789. /* Allow read access to binaries even when mode 111 */
  1790. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1791. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  1792. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  1793. err = inode_permission(inode, MAY_EXEC);
  1794. return err? nfserrno(err) : 0;
  1795. }
  1796. void
  1797. nfsd_racache_shutdown(void)
  1798. {
  1799. struct raparms *raparm, *last_raparm;
  1800. unsigned int i;
  1801. dprintk("nfsd: freeing readahead buffers.\n");
  1802. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1803. raparm = raparm_hash[i].pb_head;
  1804. while(raparm) {
  1805. last_raparm = raparm;
  1806. raparm = raparm->p_next;
  1807. kfree(last_raparm);
  1808. }
  1809. raparm_hash[i].pb_head = NULL;
  1810. }
  1811. }
  1812. /*
  1813. * Initialize readahead param cache
  1814. */
  1815. int
  1816. nfsd_racache_init(int cache_size)
  1817. {
  1818. int i;
  1819. int j = 0;
  1820. int nperbucket;
  1821. struct raparms **raparm = NULL;
  1822. if (raparm_hash[0].pb_head)
  1823. return 0;
  1824. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1825. nperbucket = max(2, nperbucket);
  1826. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1827. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1828. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1829. spin_lock_init(&raparm_hash[i].pb_lock);
  1830. raparm = &raparm_hash[i].pb_head;
  1831. for (j = 0; j < nperbucket; j++) {
  1832. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1833. if (!*raparm)
  1834. goto out_nomem;
  1835. raparm = &(*raparm)->p_next;
  1836. }
  1837. *raparm = NULL;
  1838. }
  1839. nfsdstats.ra_size = cache_size;
  1840. return 0;
  1841. out_nomem:
  1842. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1843. nfsd_racache_shutdown();
  1844. return -ENOMEM;
  1845. }