vfs.c 51 KB

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