vfs.c 47 KB

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