vfs.c 54 KB

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