vfs.c 54 KB

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