vfs.c 50 KB

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