vfs.c 50 KB

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