vfs.c 53 KB

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