vfs.c 54 KB

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