vfs.c 54 KB

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