vfs.c 56 KB

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