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