vfs.c 51 KB

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