vfs.c 51 KB

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