vfs.c 50 KB

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