vfs.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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 d_inode(dentry) != 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 && d_really_is_negative(dentry))
  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 = d_inode(fhp->fh_dentry);
  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 = d_inode(fhp->fh_dentry);
  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 = d_inode(dentry);
  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 = d_inode(dentry);
  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(&d_inode(dentry)->i_mutex);
  468. host_error = security_inode_setsecctx(dentry, label->data, label->len);
  469. mutex_unlock(&d_inode(dentry)->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 = d_inode(path.dentry);
  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. fput(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. * Obtain the readahead parameters for the file
  673. * specified by (dev, ino).
  674. */
  675. static inline struct raparms *
  676. nfsd_get_raparms(dev_t dev, ino_t ino)
  677. {
  678. struct raparms *ra, **rap, **frap = NULL;
  679. int depth = 0;
  680. unsigned int hash;
  681. struct raparm_hbucket *rab;
  682. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  683. rab = &raparm_hash[hash];
  684. spin_lock(&rab->pb_lock);
  685. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  686. if (ra->p_ino == ino && ra->p_dev == dev)
  687. goto found;
  688. depth++;
  689. if (ra->p_count == 0)
  690. frap = rap;
  691. }
  692. depth = nfsdstats.ra_size;
  693. if (!frap) {
  694. spin_unlock(&rab->pb_lock);
  695. return NULL;
  696. }
  697. rap = frap;
  698. ra = *frap;
  699. ra->p_dev = dev;
  700. ra->p_ino = ino;
  701. ra->p_set = 0;
  702. ra->p_hindex = hash;
  703. found:
  704. if (rap != &rab->pb_head) {
  705. *rap = ra->p_next;
  706. ra->p_next = rab->pb_head;
  707. rab->pb_head = ra;
  708. }
  709. ra->p_count++;
  710. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  711. spin_unlock(&rab->pb_lock);
  712. return ra;
  713. }
  714. /*
  715. * Grab and keep cached pages associated with a file in the svc_rqst
  716. * so that they can be passed to the network sendmsg/sendpage routines
  717. * directly. They will be released after the sending has completed.
  718. */
  719. static int
  720. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  721. struct splice_desc *sd)
  722. {
  723. struct svc_rqst *rqstp = sd->u.data;
  724. struct page **pp = rqstp->rq_next_page;
  725. struct page *page = buf->page;
  726. size_t size;
  727. size = sd->len;
  728. if (rqstp->rq_res.page_len == 0) {
  729. get_page(page);
  730. put_page(*rqstp->rq_next_page);
  731. *(rqstp->rq_next_page++) = page;
  732. rqstp->rq_res.page_base = buf->offset;
  733. rqstp->rq_res.page_len = size;
  734. } else if (page != pp[-1]) {
  735. get_page(page);
  736. if (*rqstp->rq_next_page)
  737. put_page(*rqstp->rq_next_page);
  738. *(rqstp->rq_next_page++) = page;
  739. rqstp->rq_res.page_len += size;
  740. } else
  741. rqstp->rq_res.page_len += size;
  742. return size;
  743. }
  744. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  745. struct splice_desc *sd)
  746. {
  747. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  748. }
  749. static __be32
  750. nfsd_finish_read(struct file *file, unsigned long *count, int host_err)
  751. {
  752. if (host_err >= 0) {
  753. nfsdstats.io_read += host_err;
  754. *count = host_err;
  755. fsnotify_access(file);
  756. return 0;
  757. } else
  758. return nfserrno(host_err);
  759. }
  760. __be32 nfsd_splice_read(struct svc_rqst *rqstp,
  761. struct file *file, loff_t offset, unsigned long *count)
  762. {
  763. struct splice_desc sd = {
  764. .len = 0,
  765. .total_len = *count,
  766. .pos = offset,
  767. .u.data = rqstp,
  768. };
  769. int host_err;
  770. rqstp->rq_next_page = rqstp->rq_respages + 1;
  771. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  772. return nfsd_finish_read(file, count, host_err);
  773. }
  774. __be32 nfsd_readv(struct file *file, loff_t offset, struct kvec *vec, int vlen,
  775. unsigned long *count)
  776. {
  777. mm_segment_t oldfs;
  778. int host_err;
  779. oldfs = get_fs();
  780. set_fs(KERNEL_DS);
  781. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  782. set_fs(oldfs);
  783. return nfsd_finish_read(file, count, host_err);
  784. }
  785. static __be32
  786. nfsd_vfs_read(struct svc_rqst *rqstp, struct file *file,
  787. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  788. {
  789. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags))
  790. return nfsd_splice_read(rqstp, file, offset, count);
  791. else
  792. return nfsd_readv(file, offset, vec, vlen, count);
  793. }
  794. /*
  795. * Gathered writes: If another process is currently writing to the file,
  796. * there's a high chance this is another nfsd (triggered by a bulk write
  797. * from a client's biod). Rather than syncing the file with each write
  798. * request, we sleep for 10 msec.
  799. *
  800. * I don't know if this roughly approximates C. Juszak's idea of
  801. * gathered writes, but it's a nice and simple solution (IMHO), and it
  802. * seems to work:-)
  803. *
  804. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  805. * better tool (separate unstable writes and commits) for solving this
  806. * problem.
  807. */
  808. static int wait_for_concurrent_writes(struct file *file)
  809. {
  810. struct inode *inode = file_inode(file);
  811. static ino_t last_ino;
  812. static dev_t last_dev;
  813. int err = 0;
  814. if (atomic_read(&inode->i_writecount) > 1
  815. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  816. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  817. msleep(10);
  818. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  819. }
  820. if (inode->i_state & I_DIRTY) {
  821. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  822. err = vfs_fsync(file, 0);
  823. }
  824. last_ino = inode->i_ino;
  825. last_dev = inode->i_sb->s_dev;
  826. return err;
  827. }
  828. static __be32
  829. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  830. loff_t offset, struct kvec *vec, int vlen,
  831. unsigned long *cnt, int *stablep)
  832. {
  833. struct svc_export *exp;
  834. struct inode *inode;
  835. mm_segment_t oldfs;
  836. __be32 err = 0;
  837. int host_err;
  838. int stable = *stablep;
  839. int use_wgather;
  840. loff_t pos = offset;
  841. loff_t end = LLONG_MAX;
  842. unsigned int pflags = current->flags;
  843. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  844. /*
  845. * We want less throttling in balance_dirty_pages()
  846. * and shrink_inactive_list() so that nfs to
  847. * localhost doesn't cause nfsd to lock up due to all
  848. * the client's dirty pages or its congested queue.
  849. */
  850. current->flags |= PF_LESS_THROTTLE;
  851. inode = file_inode(file);
  852. exp = fhp->fh_export;
  853. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  854. if (!EX_ISSYNC(exp))
  855. stable = 0;
  856. /* Write the data. */
  857. oldfs = get_fs(); set_fs(KERNEL_DS);
  858. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &pos);
  859. set_fs(oldfs);
  860. if (host_err < 0)
  861. goto out_nfserr;
  862. *cnt = host_err;
  863. nfsdstats.io_write += host_err;
  864. fsnotify_modify(file);
  865. if (stable) {
  866. if (use_wgather) {
  867. host_err = wait_for_concurrent_writes(file);
  868. } else {
  869. if (*cnt)
  870. end = offset + *cnt - 1;
  871. host_err = vfs_fsync_range(file, offset, end, 0);
  872. }
  873. }
  874. out_nfserr:
  875. dprintk("nfsd: write complete host_err=%d\n", host_err);
  876. if (host_err >= 0)
  877. err = 0;
  878. else
  879. err = nfserrno(host_err);
  880. if (test_bit(RQ_LOCAL, &rqstp->rq_flags))
  881. tsk_restore_flags(current, pflags, PF_LESS_THROTTLE);
  882. return err;
  883. }
  884. __be32 nfsd_get_tmp_read_open(struct svc_rqst *rqstp, struct svc_fh *fhp,
  885. struct file **file, struct raparms **ra)
  886. {
  887. struct inode *inode;
  888. __be32 err;
  889. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, file);
  890. if (err)
  891. return err;
  892. inode = file_inode(*file);
  893. /* Get readahead parameters */
  894. *ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  895. if (*ra && (*ra)->p_set)
  896. (*file)->f_ra = (*ra)->p_ra;
  897. return nfs_ok;
  898. }
  899. void nfsd_put_tmp_read_open(struct file *file, struct raparms *ra)
  900. {
  901. /* Write back readahead params */
  902. if (ra) {
  903. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  904. spin_lock(&rab->pb_lock);
  905. ra->p_ra = file->f_ra;
  906. ra->p_set = 1;
  907. ra->p_count--;
  908. spin_unlock(&rab->pb_lock);
  909. }
  910. fput(file);
  911. }
  912. /*
  913. * Read data from a file. count must contain the requested read count
  914. * on entry. On return, *count contains the number of bytes actually read.
  915. * N.B. After this call fhp needs an fh_put
  916. */
  917. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  918. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  919. {
  920. struct file *file;
  921. struct raparms *ra;
  922. __be32 err;
  923. err = nfsd_get_tmp_read_open(rqstp, fhp, &file, &ra);
  924. if (err)
  925. return err;
  926. err = nfsd_vfs_read(rqstp, file, offset, vec, vlen, count);
  927. nfsd_put_tmp_read_open(file, ra);
  928. return err;
  929. }
  930. /*
  931. * Write data to a file.
  932. * The stable flag requests synchronous writes.
  933. * N.B. After this call fhp needs an fh_put
  934. */
  935. __be32
  936. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  937. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  938. int *stablep)
  939. {
  940. __be32 err = 0;
  941. if (file) {
  942. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  943. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  944. if (err)
  945. goto out;
  946. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  947. stablep);
  948. } else {
  949. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  950. if (err)
  951. goto out;
  952. if (cnt)
  953. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  954. cnt, stablep);
  955. fput(file);
  956. }
  957. out:
  958. return err;
  959. }
  960. #ifdef CONFIG_NFSD_V3
  961. /*
  962. * Commit all pending writes to stable storage.
  963. *
  964. * Note: we only guarantee that data that lies within the range specified
  965. * by the 'offset' and 'count' parameters will be synced.
  966. *
  967. * Unfortunately we cannot lock the file to make sure we return full WCC
  968. * data to the client, as locking happens lower down in the filesystem.
  969. */
  970. __be32
  971. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  972. loff_t offset, unsigned long count)
  973. {
  974. struct file *file;
  975. loff_t end = LLONG_MAX;
  976. __be32 err = nfserr_inval;
  977. if (offset < 0)
  978. goto out;
  979. if (count != 0) {
  980. end = offset + (loff_t)count - 1;
  981. if (end < offset)
  982. goto out;
  983. }
  984. err = nfsd_open(rqstp, fhp, S_IFREG,
  985. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  986. if (err)
  987. goto out;
  988. if (EX_ISSYNC(fhp->fh_export)) {
  989. int err2 = vfs_fsync_range(file, offset, end, 0);
  990. if (err2 != -EINVAL)
  991. err = nfserrno(err2);
  992. else
  993. err = nfserr_notsupp;
  994. }
  995. fput(file);
  996. out:
  997. return err;
  998. }
  999. #endif /* CONFIG_NFSD_V3 */
  1000. static __be32
  1001. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1002. struct iattr *iap)
  1003. {
  1004. /*
  1005. * Mode has already been set earlier in create:
  1006. */
  1007. iap->ia_valid &= ~ATTR_MODE;
  1008. /*
  1009. * Setting uid/gid works only for root. Irix appears to
  1010. * send along the gid on create when it tries to implement
  1011. * setgid directories via NFS:
  1012. */
  1013. if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  1014. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1015. if (iap->ia_valid)
  1016. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1017. /* Callers expect file metadata to be committed here */
  1018. return nfserrno(commit_metadata(resfhp));
  1019. }
  1020. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1021. * setting size to 0 may fail for some specific file systems by the permission
  1022. * checking which requires WRITE permission but the mode is 000.
  1023. * we ignore the resizing(to 0) on the just new created file, since the size is
  1024. * 0 after file created.
  1025. *
  1026. * call this only after vfs_create() is called.
  1027. * */
  1028. static void
  1029. nfsd_check_ignore_resizing(struct iattr *iap)
  1030. {
  1031. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1032. iap->ia_valid &= ~ATTR_SIZE;
  1033. }
  1034. /*
  1035. * Create a file (regular, directory, device, fifo); UNIX sockets
  1036. * not yet implemented.
  1037. * If the response fh has been verified, the parent directory should
  1038. * already be locked. Note that the parent directory is left locked.
  1039. *
  1040. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1041. */
  1042. __be32
  1043. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1044. char *fname, int flen, struct iattr *iap,
  1045. int type, dev_t rdev, struct svc_fh *resfhp)
  1046. {
  1047. struct dentry *dentry, *dchild = NULL;
  1048. struct inode *dirp;
  1049. __be32 err;
  1050. __be32 err2;
  1051. int host_err;
  1052. err = nfserr_perm;
  1053. if (!flen)
  1054. goto out;
  1055. err = nfserr_exist;
  1056. if (isdotent(fname, flen))
  1057. goto out;
  1058. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1059. if (err)
  1060. goto out;
  1061. dentry = fhp->fh_dentry;
  1062. dirp = d_inode(dentry);
  1063. err = nfserr_notdir;
  1064. if (!dirp->i_op->lookup)
  1065. goto out;
  1066. /*
  1067. * Check whether the response file handle has been verified yet.
  1068. * If it has, the parent directory should already be locked.
  1069. */
  1070. if (!resfhp->fh_dentry) {
  1071. host_err = fh_want_write(fhp);
  1072. if (host_err)
  1073. goto out_nfserr;
  1074. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1075. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1076. dchild = lookup_one_len(fname, dentry, flen);
  1077. host_err = PTR_ERR(dchild);
  1078. if (IS_ERR(dchild))
  1079. goto out_nfserr;
  1080. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1081. if (err)
  1082. goto out;
  1083. } else {
  1084. /* called from nfsd_proc_create */
  1085. dchild = dget(resfhp->fh_dentry);
  1086. if (!fhp->fh_locked) {
  1087. /* not actually possible */
  1088. printk(KERN_ERR
  1089. "nfsd_create: parent %pd2 not locked!\n",
  1090. dentry);
  1091. err = nfserr_io;
  1092. goto out;
  1093. }
  1094. }
  1095. /*
  1096. * Make sure the child dentry is still negative ...
  1097. */
  1098. err = nfserr_exist;
  1099. if (d_really_is_positive(dchild)) {
  1100. dprintk("nfsd_create: dentry %pd/%pd not negative!\n",
  1101. dentry, dchild);
  1102. goto out;
  1103. }
  1104. if (!(iap->ia_valid & ATTR_MODE))
  1105. iap->ia_mode = 0;
  1106. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1107. err = nfserr_inval;
  1108. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1109. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1110. type);
  1111. goto out;
  1112. }
  1113. /*
  1114. * Get the dir op function pointer.
  1115. */
  1116. err = 0;
  1117. host_err = 0;
  1118. switch (type) {
  1119. case S_IFREG:
  1120. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1121. if (!host_err)
  1122. nfsd_check_ignore_resizing(iap);
  1123. break;
  1124. case S_IFDIR:
  1125. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1126. break;
  1127. case S_IFCHR:
  1128. case S_IFBLK:
  1129. case S_IFIFO:
  1130. case S_IFSOCK:
  1131. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1132. break;
  1133. }
  1134. if (host_err < 0)
  1135. goto out_nfserr;
  1136. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1137. /*
  1138. * nfsd_create_setattr already committed the child. Transactional
  1139. * filesystems had a chance to commit changes for both parent and
  1140. * child * simultaneously making the following commit_metadata a
  1141. * noop.
  1142. */
  1143. err2 = nfserrno(commit_metadata(fhp));
  1144. if (err2)
  1145. err = err2;
  1146. /*
  1147. * Update the file handle to get the new inode info.
  1148. */
  1149. if (!err)
  1150. err = fh_update(resfhp);
  1151. out:
  1152. if (dchild && !IS_ERR(dchild))
  1153. dput(dchild);
  1154. return err;
  1155. out_nfserr:
  1156. err = nfserrno(host_err);
  1157. goto out;
  1158. }
  1159. #ifdef CONFIG_NFSD_V3
  1160. static inline int nfsd_create_is_exclusive(int createmode)
  1161. {
  1162. return createmode == NFS3_CREATE_EXCLUSIVE
  1163. || createmode == NFS4_CREATE_EXCLUSIVE4_1;
  1164. }
  1165. /*
  1166. * NFSv3 and NFSv4 version of nfsd_create
  1167. */
  1168. __be32
  1169. do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1170. char *fname, int flen, struct iattr *iap,
  1171. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1172. bool *truncp, bool *created)
  1173. {
  1174. struct dentry *dentry, *dchild = NULL;
  1175. struct inode *dirp;
  1176. __be32 err;
  1177. int host_err;
  1178. __u32 v_mtime=0, v_atime=0;
  1179. err = nfserr_perm;
  1180. if (!flen)
  1181. goto out;
  1182. err = nfserr_exist;
  1183. if (isdotent(fname, flen))
  1184. goto out;
  1185. if (!(iap->ia_valid & ATTR_MODE))
  1186. iap->ia_mode = 0;
  1187. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  1188. if (err)
  1189. goto out;
  1190. dentry = fhp->fh_dentry;
  1191. dirp = d_inode(dentry);
  1192. /* Get all the sanity checks out of the way before
  1193. * we lock the parent. */
  1194. err = nfserr_notdir;
  1195. if (!dirp->i_op->lookup)
  1196. goto out;
  1197. host_err = fh_want_write(fhp);
  1198. if (host_err)
  1199. goto out_nfserr;
  1200. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1201. /*
  1202. * Compose the response file handle.
  1203. */
  1204. dchild = lookup_one_len(fname, dentry, flen);
  1205. host_err = PTR_ERR(dchild);
  1206. if (IS_ERR(dchild))
  1207. goto out_nfserr;
  1208. /* If file doesn't exist, check for permissions to create one */
  1209. if (d_really_is_negative(dchild)) {
  1210. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1211. if (err)
  1212. goto out;
  1213. }
  1214. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1215. if (err)
  1216. goto out;
  1217. if (nfsd_create_is_exclusive(createmode)) {
  1218. /* solaris7 gets confused (bugid 4218508) if these have
  1219. * the high bit set, so just clear the high bits. If this is
  1220. * ever changed to use different attrs for storing the
  1221. * verifier, then do_open_lookup() will also need to be fixed
  1222. * accordingly.
  1223. */
  1224. v_mtime = verifier[0]&0x7fffffff;
  1225. v_atime = verifier[1]&0x7fffffff;
  1226. }
  1227. if (d_really_is_positive(dchild)) {
  1228. err = 0;
  1229. switch (createmode) {
  1230. case NFS3_CREATE_UNCHECKED:
  1231. if (! d_is_reg(dchild))
  1232. goto out;
  1233. else if (truncp) {
  1234. /* in nfsv4, we need to treat this case a little
  1235. * differently. we don't want to truncate the
  1236. * file now; this would be wrong if the OPEN
  1237. * fails for some other reason. furthermore,
  1238. * if the size is nonzero, we should ignore it
  1239. * according to spec!
  1240. */
  1241. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1242. }
  1243. else {
  1244. iap->ia_valid &= ATTR_SIZE;
  1245. goto set_attr;
  1246. }
  1247. break;
  1248. case NFS3_CREATE_EXCLUSIVE:
  1249. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1250. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1251. && d_inode(dchild)->i_size == 0 ) {
  1252. if (created)
  1253. *created = 1;
  1254. break;
  1255. }
  1256. case NFS4_CREATE_EXCLUSIVE4_1:
  1257. if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime
  1258. && d_inode(dchild)->i_atime.tv_sec == v_atime
  1259. && d_inode(dchild)->i_size == 0 ) {
  1260. if (created)
  1261. *created = 1;
  1262. goto set_attr;
  1263. }
  1264. /* fallthru */
  1265. case NFS3_CREATE_GUARDED:
  1266. err = nfserr_exist;
  1267. }
  1268. fh_drop_write(fhp);
  1269. goto out;
  1270. }
  1271. host_err = vfs_create(dirp, dchild, iap->ia_mode, true);
  1272. if (host_err < 0) {
  1273. fh_drop_write(fhp);
  1274. goto out_nfserr;
  1275. }
  1276. if (created)
  1277. *created = 1;
  1278. nfsd_check_ignore_resizing(iap);
  1279. if (nfsd_create_is_exclusive(createmode)) {
  1280. /* Cram the verifier into atime/mtime */
  1281. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1282. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1283. /* XXX someone who knows this better please fix it for nsec */
  1284. iap->ia_mtime.tv_sec = v_mtime;
  1285. iap->ia_atime.tv_sec = v_atime;
  1286. iap->ia_mtime.tv_nsec = 0;
  1287. iap->ia_atime.tv_nsec = 0;
  1288. }
  1289. set_attr:
  1290. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1291. /*
  1292. * nfsd_create_setattr already committed the child
  1293. * (and possibly also the parent).
  1294. */
  1295. if (!err)
  1296. err = nfserrno(commit_metadata(fhp));
  1297. /*
  1298. * Update the filehandle to get the new inode info.
  1299. */
  1300. if (!err)
  1301. err = fh_update(resfhp);
  1302. out:
  1303. fh_unlock(fhp);
  1304. if (dchild && !IS_ERR(dchild))
  1305. dput(dchild);
  1306. fh_drop_write(fhp);
  1307. return err;
  1308. out_nfserr:
  1309. err = nfserrno(host_err);
  1310. goto out;
  1311. }
  1312. #endif /* CONFIG_NFSD_V3 */
  1313. /*
  1314. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1315. * fits into the buffer. On return, it contains the true length.
  1316. * N.B. After this call fhp needs an fh_put
  1317. */
  1318. __be32
  1319. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1320. {
  1321. struct inode *inode;
  1322. mm_segment_t oldfs;
  1323. __be32 err;
  1324. int host_err;
  1325. struct path path;
  1326. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1327. if (err)
  1328. goto out;
  1329. path.mnt = fhp->fh_export->ex_path.mnt;
  1330. path.dentry = fhp->fh_dentry;
  1331. inode = d_inode(path.dentry);
  1332. err = nfserr_inval;
  1333. if (!inode->i_op->readlink)
  1334. goto out;
  1335. touch_atime(&path);
  1336. /* N.B. Why does this call need a get_fs()??
  1337. * Remove the set_fs and watch the fireworks:-) --okir
  1338. */
  1339. oldfs = get_fs(); set_fs(KERNEL_DS);
  1340. host_err = inode->i_op->readlink(path.dentry, (char __user *)buf, *lenp);
  1341. set_fs(oldfs);
  1342. if (host_err < 0)
  1343. goto out_nfserr;
  1344. *lenp = host_err;
  1345. err = 0;
  1346. out:
  1347. return err;
  1348. out_nfserr:
  1349. err = nfserrno(host_err);
  1350. goto out;
  1351. }
  1352. /*
  1353. * Create a symlink and look up its inode
  1354. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1355. */
  1356. __be32
  1357. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1358. char *fname, int flen,
  1359. char *path,
  1360. struct svc_fh *resfhp)
  1361. {
  1362. struct dentry *dentry, *dnew;
  1363. __be32 err, cerr;
  1364. int host_err;
  1365. err = nfserr_noent;
  1366. if (!flen || path[0] == '\0')
  1367. goto out;
  1368. err = nfserr_exist;
  1369. if (isdotent(fname, flen))
  1370. goto out;
  1371. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1372. if (err)
  1373. goto out;
  1374. host_err = fh_want_write(fhp);
  1375. if (host_err)
  1376. goto out_nfserr;
  1377. fh_lock(fhp);
  1378. dentry = fhp->fh_dentry;
  1379. dnew = lookup_one_len(fname, dentry, flen);
  1380. host_err = PTR_ERR(dnew);
  1381. if (IS_ERR(dnew))
  1382. goto out_nfserr;
  1383. host_err = vfs_symlink(d_inode(dentry), dnew, path);
  1384. err = nfserrno(host_err);
  1385. if (!err)
  1386. err = nfserrno(commit_metadata(fhp));
  1387. fh_unlock(fhp);
  1388. fh_drop_write(fhp);
  1389. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1390. dput(dnew);
  1391. if (err==0) err = cerr;
  1392. out:
  1393. return err;
  1394. out_nfserr:
  1395. err = nfserrno(host_err);
  1396. goto out;
  1397. }
  1398. /*
  1399. * Create a hardlink
  1400. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1401. */
  1402. __be32
  1403. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1404. char *name, int len, struct svc_fh *tfhp)
  1405. {
  1406. struct dentry *ddir, *dnew, *dold;
  1407. struct inode *dirp;
  1408. __be32 err;
  1409. int host_err;
  1410. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1411. if (err)
  1412. goto out;
  1413. err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP);
  1414. if (err)
  1415. goto out;
  1416. err = nfserr_isdir;
  1417. if (d_is_dir(tfhp->fh_dentry))
  1418. goto out;
  1419. err = nfserr_perm;
  1420. if (!len)
  1421. goto out;
  1422. err = nfserr_exist;
  1423. if (isdotent(name, len))
  1424. goto out;
  1425. host_err = fh_want_write(tfhp);
  1426. if (host_err) {
  1427. err = nfserrno(host_err);
  1428. goto out;
  1429. }
  1430. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1431. ddir = ffhp->fh_dentry;
  1432. dirp = d_inode(ddir);
  1433. dnew = lookup_one_len(name, ddir, len);
  1434. host_err = PTR_ERR(dnew);
  1435. if (IS_ERR(dnew))
  1436. goto out_nfserr;
  1437. dold = tfhp->fh_dentry;
  1438. err = nfserr_noent;
  1439. if (d_really_is_negative(dold))
  1440. goto out_dput;
  1441. host_err = vfs_link(dold, dirp, dnew, NULL);
  1442. if (!host_err) {
  1443. err = nfserrno(commit_metadata(ffhp));
  1444. if (!err)
  1445. err = nfserrno(commit_metadata(tfhp));
  1446. } else {
  1447. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1448. err = nfserr_acces;
  1449. else
  1450. err = nfserrno(host_err);
  1451. }
  1452. out_dput:
  1453. dput(dnew);
  1454. out_unlock:
  1455. fh_unlock(ffhp);
  1456. fh_drop_write(tfhp);
  1457. out:
  1458. return err;
  1459. out_nfserr:
  1460. err = nfserrno(host_err);
  1461. goto out_unlock;
  1462. }
  1463. /*
  1464. * Rename a file
  1465. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1466. */
  1467. __be32
  1468. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1469. struct svc_fh *tfhp, char *tname, int tlen)
  1470. {
  1471. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1472. struct inode *fdir, *tdir;
  1473. __be32 err;
  1474. int host_err;
  1475. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1476. if (err)
  1477. goto out;
  1478. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1479. if (err)
  1480. goto out;
  1481. fdentry = ffhp->fh_dentry;
  1482. fdir = d_inode(fdentry);
  1483. tdentry = tfhp->fh_dentry;
  1484. tdir = d_inode(tdentry);
  1485. err = nfserr_perm;
  1486. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1487. goto out;
  1488. host_err = fh_want_write(ffhp);
  1489. if (host_err) {
  1490. err = nfserrno(host_err);
  1491. goto out;
  1492. }
  1493. /* cannot use fh_lock as we need deadlock protective ordering
  1494. * so do it by hand */
  1495. trap = lock_rename(tdentry, fdentry);
  1496. ffhp->fh_locked = tfhp->fh_locked = 1;
  1497. fill_pre_wcc(ffhp);
  1498. fill_pre_wcc(tfhp);
  1499. odentry = lookup_one_len(fname, fdentry, flen);
  1500. host_err = PTR_ERR(odentry);
  1501. if (IS_ERR(odentry))
  1502. goto out_nfserr;
  1503. host_err = -ENOENT;
  1504. if (d_really_is_negative(odentry))
  1505. goto out_dput_old;
  1506. host_err = -EINVAL;
  1507. if (odentry == trap)
  1508. goto out_dput_old;
  1509. ndentry = lookup_one_len(tname, tdentry, tlen);
  1510. host_err = PTR_ERR(ndentry);
  1511. if (IS_ERR(ndentry))
  1512. goto out_dput_old;
  1513. host_err = -ENOTEMPTY;
  1514. if (ndentry == trap)
  1515. goto out_dput_new;
  1516. host_err = -EXDEV;
  1517. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1518. goto out_dput_new;
  1519. if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry)
  1520. goto out_dput_new;
  1521. host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0);
  1522. if (!host_err) {
  1523. host_err = commit_metadata(tfhp);
  1524. if (!host_err)
  1525. host_err = commit_metadata(ffhp);
  1526. }
  1527. out_dput_new:
  1528. dput(ndentry);
  1529. out_dput_old:
  1530. dput(odentry);
  1531. out_nfserr:
  1532. err = nfserrno(host_err);
  1533. /*
  1534. * We cannot rely on fh_unlock on the two filehandles,
  1535. * as that would do the wrong thing if the two directories
  1536. * were the same, so again we do it by hand.
  1537. */
  1538. fill_post_wcc(ffhp);
  1539. fill_post_wcc(tfhp);
  1540. unlock_rename(tdentry, fdentry);
  1541. ffhp->fh_locked = tfhp->fh_locked = 0;
  1542. fh_drop_write(ffhp);
  1543. out:
  1544. return err;
  1545. }
  1546. /*
  1547. * Unlink a file or directory
  1548. * N.B. After this call fhp needs an fh_put
  1549. */
  1550. __be32
  1551. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1552. char *fname, int flen)
  1553. {
  1554. struct dentry *dentry, *rdentry;
  1555. struct inode *dirp;
  1556. __be32 err;
  1557. int host_err;
  1558. err = nfserr_acces;
  1559. if (!flen || isdotent(fname, flen))
  1560. goto out;
  1561. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1562. if (err)
  1563. goto out;
  1564. host_err = fh_want_write(fhp);
  1565. if (host_err)
  1566. goto out_nfserr;
  1567. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1568. dentry = fhp->fh_dentry;
  1569. dirp = d_inode(dentry);
  1570. rdentry = lookup_one_len(fname, dentry, flen);
  1571. host_err = PTR_ERR(rdentry);
  1572. if (IS_ERR(rdentry))
  1573. goto out_nfserr;
  1574. if (d_really_is_negative(rdentry)) {
  1575. dput(rdentry);
  1576. err = nfserr_noent;
  1577. goto out;
  1578. }
  1579. if (!type)
  1580. type = d_inode(rdentry)->i_mode & S_IFMT;
  1581. if (type != S_IFDIR)
  1582. host_err = vfs_unlink(dirp, rdentry, NULL);
  1583. else
  1584. host_err = vfs_rmdir(dirp, rdentry);
  1585. if (!host_err)
  1586. host_err = commit_metadata(fhp);
  1587. dput(rdentry);
  1588. out_nfserr:
  1589. err = nfserrno(host_err);
  1590. out:
  1591. return err;
  1592. }
  1593. /*
  1594. * We do this buffering because we must not call back into the file
  1595. * system's ->lookup() method from the filldir callback. That may well
  1596. * deadlock a number of file systems.
  1597. *
  1598. * This is based heavily on the implementation of same in XFS.
  1599. */
  1600. struct buffered_dirent {
  1601. u64 ino;
  1602. loff_t offset;
  1603. int namlen;
  1604. unsigned int d_type;
  1605. char name[];
  1606. };
  1607. struct readdir_data {
  1608. struct dir_context ctx;
  1609. char *dirent;
  1610. size_t used;
  1611. int full;
  1612. };
  1613. static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name,
  1614. int namlen, loff_t offset, u64 ino,
  1615. unsigned int d_type)
  1616. {
  1617. struct readdir_data *buf =
  1618. container_of(ctx, struct readdir_data, ctx);
  1619. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1620. unsigned int reclen;
  1621. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1622. if (buf->used + reclen > PAGE_SIZE) {
  1623. buf->full = 1;
  1624. return -EINVAL;
  1625. }
  1626. de->namlen = namlen;
  1627. de->offset = offset;
  1628. de->ino = ino;
  1629. de->d_type = d_type;
  1630. memcpy(de->name, name, namlen);
  1631. buf->used += reclen;
  1632. return 0;
  1633. }
  1634. static __be32 nfsd_buffered_readdir(struct file *file, nfsd_filldir_t func,
  1635. struct readdir_cd *cdp, loff_t *offsetp)
  1636. {
  1637. struct buffered_dirent *de;
  1638. int host_err;
  1639. int size;
  1640. loff_t offset;
  1641. struct readdir_data buf = {
  1642. .ctx.actor = nfsd_buffered_filldir,
  1643. .dirent = (void *)__get_free_page(GFP_KERNEL)
  1644. };
  1645. if (!buf.dirent)
  1646. return nfserrno(-ENOMEM);
  1647. offset = *offsetp;
  1648. while (1) {
  1649. struct inode *dir_inode = file_inode(file);
  1650. unsigned int reclen;
  1651. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1652. buf.used = 0;
  1653. buf.full = 0;
  1654. host_err = iterate_dir(file, &buf.ctx);
  1655. if (buf.full)
  1656. host_err = 0;
  1657. if (host_err < 0)
  1658. break;
  1659. size = buf.used;
  1660. if (!size)
  1661. break;
  1662. /*
  1663. * Various filldir functions may end up calling back into
  1664. * lookup_one_len() and the file system's ->lookup() method.
  1665. * These expect i_mutex to be held, as it would within readdir.
  1666. */
  1667. host_err = mutex_lock_killable(&dir_inode->i_mutex);
  1668. if (host_err)
  1669. break;
  1670. de = (struct buffered_dirent *)buf.dirent;
  1671. while (size > 0) {
  1672. offset = de->offset;
  1673. if (func(cdp, de->name, de->namlen, de->offset,
  1674. de->ino, de->d_type))
  1675. break;
  1676. if (cdp->err != nfs_ok)
  1677. break;
  1678. reclen = ALIGN(sizeof(*de) + de->namlen,
  1679. sizeof(u64));
  1680. size -= reclen;
  1681. de = (struct buffered_dirent *)((char *)de + reclen);
  1682. }
  1683. mutex_unlock(&dir_inode->i_mutex);
  1684. if (size > 0) /* We bailed out early */
  1685. break;
  1686. offset = vfs_llseek(file, 0, SEEK_CUR);
  1687. }
  1688. free_page((unsigned long)(buf.dirent));
  1689. if (host_err)
  1690. return nfserrno(host_err);
  1691. *offsetp = offset;
  1692. return cdp->err;
  1693. }
  1694. /*
  1695. * Read entries from a directory.
  1696. * The NFSv3/4 verifier we ignore for now.
  1697. */
  1698. __be32
  1699. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1700. struct readdir_cd *cdp, nfsd_filldir_t func)
  1701. {
  1702. __be32 err;
  1703. struct file *file;
  1704. loff_t offset = *offsetp;
  1705. int may_flags = NFSD_MAY_READ;
  1706. /* NFSv2 only supports 32 bit cookies */
  1707. if (rqstp->rq_vers > 2)
  1708. may_flags |= NFSD_MAY_64BIT_COOKIE;
  1709. err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file);
  1710. if (err)
  1711. goto out;
  1712. offset = vfs_llseek(file, offset, SEEK_SET);
  1713. if (offset < 0) {
  1714. err = nfserrno((int)offset);
  1715. goto out_close;
  1716. }
  1717. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1718. if (err == nfserr_eof || err == nfserr_toosmall)
  1719. err = nfs_ok; /* can still be found in ->err */
  1720. out_close:
  1721. fput(file);
  1722. out:
  1723. return err;
  1724. }
  1725. /*
  1726. * Get file system stats
  1727. * N.B. After this call fhp needs an fh_put
  1728. */
  1729. __be32
  1730. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1731. {
  1732. __be32 err;
  1733. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1734. if (!err) {
  1735. struct path path = {
  1736. .mnt = fhp->fh_export->ex_path.mnt,
  1737. .dentry = fhp->fh_dentry,
  1738. };
  1739. if (vfs_statfs(&path, stat))
  1740. err = nfserr_io;
  1741. }
  1742. return err;
  1743. }
  1744. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1745. {
  1746. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1747. }
  1748. /*
  1749. * Check for a user's access permissions to this inode.
  1750. */
  1751. __be32
  1752. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1753. struct dentry *dentry, int acc)
  1754. {
  1755. struct inode *inode = d_inode(dentry);
  1756. int err;
  1757. if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP)
  1758. return 0;
  1759. #if 0
  1760. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1761. acc,
  1762. (acc & NFSD_MAY_READ)? " read" : "",
  1763. (acc & NFSD_MAY_WRITE)? " write" : "",
  1764. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1765. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1766. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1767. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1768. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1769. inode->i_mode,
  1770. IS_IMMUTABLE(inode)? " immut" : "",
  1771. IS_APPEND(inode)? " append" : "",
  1772. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1773. dprintk(" owner %d/%d user %d/%d\n",
  1774. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1775. #endif
  1776. /* Normally we reject any write/sattr etc access on a read-only file
  1777. * system. But if it is IRIX doing check on write-access for a
  1778. * device special file, we ignore rofs.
  1779. */
  1780. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1781. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1782. if (exp_rdonly(rqstp, exp) ||
  1783. __mnt_is_readonly(exp->ex_path.mnt))
  1784. return nfserr_rofs;
  1785. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1786. return nfserr_perm;
  1787. }
  1788. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1789. return nfserr_perm;
  1790. if (acc & NFSD_MAY_LOCK) {
  1791. /* If we cannot rely on authentication in NLM requests,
  1792. * just allow locks, otherwise require read permission, or
  1793. * ownership
  1794. */
  1795. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1796. return 0;
  1797. else
  1798. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1799. }
  1800. /*
  1801. * The file owner always gets access permission for accesses that
  1802. * would normally be checked at open time. This is to make
  1803. * file access work even when the client has done a fchmod(fd, 0).
  1804. *
  1805. * However, `cp foo bar' should fail nevertheless when bar is
  1806. * readonly. A sensible way to do this might be to reject all
  1807. * attempts to truncate a read-only file, because a creat() call
  1808. * always implies file truncation.
  1809. * ... but this isn't really fair. A process may reasonably call
  1810. * ftruncate on an open file descriptor on a file with perm 000.
  1811. * We must trust the client to do permission checking - using "ACCESS"
  1812. * with NFSv3.
  1813. */
  1814. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1815. uid_eq(inode->i_uid, current_fsuid()))
  1816. return 0;
  1817. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1818. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1819. /* Allow read access to binaries even when mode 111 */
  1820. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1821. (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) ||
  1822. acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC)))
  1823. err = inode_permission(inode, MAY_EXEC);
  1824. return err? nfserrno(err) : 0;
  1825. }
  1826. void
  1827. nfsd_racache_shutdown(void)
  1828. {
  1829. struct raparms *raparm, *last_raparm;
  1830. unsigned int i;
  1831. dprintk("nfsd: freeing readahead buffers.\n");
  1832. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1833. raparm = raparm_hash[i].pb_head;
  1834. while(raparm) {
  1835. last_raparm = raparm;
  1836. raparm = raparm->p_next;
  1837. kfree(last_raparm);
  1838. }
  1839. raparm_hash[i].pb_head = NULL;
  1840. }
  1841. }
  1842. /*
  1843. * Initialize readahead param cache
  1844. */
  1845. int
  1846. nfsd_racache_init(int cache_size)
  1847. {
  1848. int i;
  1849. int j = 0;
  1850. int nperbucket;
  1851. struct raparms **raparm = NULL;
  1852. if (raparm_hash[0].pb_head)
  1853. return 0;
  1854. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1855. nperbucket = max(2, nperbucket);
  1856. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1857. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1858. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1859. spin_lock_init(&raparm_hash[i].pb_lock);
  1860. raparm = &raparm_hash[i].pb_head;
  1861. for (j = 0; j < nperbucket; j++) {
  1862. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1863. if (!*raparm)
  1864. goto out_nomem;
  1865. raparm = &(*raparm)->p_next;
  1866. }
  1867. *raparm = NULL;
  1868. }
  1869. nfsdstats.ra_size = cache_size;
  1870. return 0;
  1871. out_nomem:
  1872. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1873. nfsd_racache_shutdown();
  1874. return -ENOMEM;
  1875. }