vfs.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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/pipe_fs_i.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 associated with a file in the svc_rqst
  706. * so that they can be passed to the network sendmsg/sendpage routines
  707. * directly. They will be released after the sending has completed.
  708. */
  709. static int
  710. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  711. struct splice_desc *sd)
  712. {
  713. struct svc_rqst *rqstp = sd->u.data;
  714. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  715. struct page *page = buf->page;
  716. size_t size;
  717. int ret;
  718. ret = buf->ops->pin(pipe, buf);
  719. if (unlikely(ret))
  720. return ret;
  721. size = sd->len;
  722. if (rqstp->rq_res.page_len == 0) {
  723. get_page(page);
  724. put_page(*pp);
  725. *pp = page;
  726. rqstp->rq_resused++;
  727. rqstp->rq_res.page_base = buf->offset;
  728. rqstp->rq_res.page_len = size;
  729. } else if (page != pp[-1]) {
  730. get_page(page);
  731. if (*pp)
  732. put_page(*pp);
  733. *pp = page;
  734. rqstp->rq_resused++;
  735. rqstp->rq_res.page_len += size;
  736. } else
  737. rqstp->rq_res.page_len += size;
  738. return size;
  739. }
  740. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  741. struct splice_desc *sd)
  742. {
  743. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  744. }
  745. static __be32
  746. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  747. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  748. {
  749. struct inode *inode;
  750. struct raparms *ra;
  751. mm_segment_t oldfs;
  752. __be32 err;
  753. int host_err;
  754. err = nfserr_perm;
  755. inode = file->f_path.dentry->d_inode;
  756. #ifdef MSNFS
  757. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  758. (!lock_may_read(inode, offset, *count)))
  759. goto out;
  760. #endif
  761. /* Get readahead parameters */
  762. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  763. if (ra && ra->p_set)
  764. file->f_ra = ra->p_ra;
  765. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  766. struct splice_desc sd = {
  767. .len = 0,
  768. .total_len = *count,
  769. .pos = offset,
  770. .u.data = rqstp,
  771. };
  772. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  773. } else {
  774. oldfs = get_fs();
  775. set_fs(KERNEL_DS);
  776. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  777. set_fs(oldfs);
  778. }
  779. /* Write back readahead params */
  780. if (ra) {
  781. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  782. spin_lock(&rab->pb_lock);
  783. ra->p_ra = file->f_ra;
  784. ra->p_set = 1;
  785. ra->p_count--;
  786. spin_unlock(&rab->pb_lock);
  787. }
  788. if (host_err >= 0) {
  789. nfsdstats.io_read += host_err;
  790. *count = host_err;
  791. err = 0;
  792. fsnotify_access(file->f_path.dentry);
  793. } else
  794. err = nfserrno(host_err);
  795. out:
  796. return err;
  797. }
  798. static void kill_suid(struct dentry *dentry)
  799. {
  800. struct iattr ia;
  801. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
  802. mutex_lock(&dentry->d_inode->i_mutex);
  803. notify_change(dentry, &ia);
  804. mutex_unlock(&dentry->d_inode->i_mutex);
  805. }
  806. static __be32
  807. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  808. loff_t offset, struct kvec *vec, int vlen,
  809. unsigned long cnt, int *stablep)
  810. {
  811. struct svc_export *exp;
  812. struct dentry *dentry;
  813. struct inode *inode;
  814. mm_segment_t oldfs;
  815. __be32 err = 0;
  816. int host_err;
  817. int stable = *stablep;
  818. #ifdef MSNFS
  819. err = nfserr_perm;
  820. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  821. (!lock_may_write(file->f_path.dentry->d_inode, offset, cnt)))
  822. goto out;
  823. #endif
  824. dentry = file->f_path.dentry;
  825. inode = dentry->d_inode;
  826. exp = fhp->fh_export;
  827. /*
  828. * Request sync writes if
  829. * - the sync export option has been set, or
  830. * - the client requested O_SYNC behavior (NFSv3 feature).
  831. * - The file system doesn't support fsync().
  832. * When gathered writes have been configured for this volume,
  833. * flushing the data to disk is handled separately below.
  834. */
  835. if (file->f_op->fsync == 0) {/* COMMIT3 cannot work */
  836. stable = 2;
  837. *stablep = 2; /* FILE_SYNC */
  838. }
  839. if (!EX_ISSYNC(exp))
  840. stable = 0;
  841. if (stable && !EX_WGATHER(exp))
  842. file->f_flags |= O_SYNC;
  843. /* Write the data. */
  844. oldfs = get_fs(); set_fs(KERNEL_DS);
  845. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  846. set_fs(oldfs);
  847. if (host_err >= 0) {
  848. nfsdstats.io_write += cnt;
  849. fsnotify_modify(file->f_path.dentry);
  850. }
  851. /* clear setuid/setgid flag after write */
  852. if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  853. kill_suid(dentry);
  854. if (host_err >= 0 && stable) {
  855. static ino_t last_ino;
  856. static dev_t last_dev;
  857. /*
  858. * Gathered writes: If another process is currently
  859. * writing to the file, there's a high chance
  860. * this is another nfsd (triggered by a bulk write
  861. * from a client's biod). Rather than syncing the
  862. * file with each write request, we sleep for 10 msec.
  863. *
  864. * I don't know if this roughly approximates
  865. * C. Juszak's idea of gathered writes, but it's a
  866. * nice and simple solution (IMHO), and it seems to
  867. * work:-)
  868. */
  869. if (EX_WGATHER(exp)) {
  870. if (atomic_read(&inode->i_writecount) > 1
  871. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  872. dprintk("nfsd: write defer %d\n", current->pid);
  873. msleep(10);
  874. dprintk("nfsd: write resume %d\n", current->pid);
  875. }
  876. if (inode->i_state & I_DIRTY) {
  877. dprintk("nfsd: write sync %d\n", current->pid);
  878. host_err=nfsd_sync(file);
  879. }
  880. #if 0
  881. wake_up(&inode->i_wait);
  882. #endif
  883. }
  884. last_ino = inode->i_ino;
  885. last_dev = inode->i_sb->s_dev;
  886. }
  887. dprintk("nfsd: write complete host_err=%d\n", host_err);
  888. if (host_err >= 0)
  889. err = 0;
  890. else
  891. err = nfserrno(host_err);
  892. out:
  893. return err;
  894. }
  895. /*
  896. * Read data from a file. count must contain the requested read count
  897. * on entry. On return, *count contains the number of bytes actually read.
  898. * N.B. After this call fhp needs an fh_put
  899. */
  900. __be32
  901. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  902. loff_t offset, struct kvec *vec, int vlen,
  903. unsigned long *count)
  904. {
  905. __be32 err;
  906. if (file) {
  907. err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
  908. MAY_READ|MAY_OWNER_OVERRIDE);
  909. if (err)
  910. goto out;
  911. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  912. } else {
  913. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_READ, &file);
  914. if (err)
  915. goto out;
  916. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  917. nfsd_close(file);
  918. }
  919. out:
  920. return err;
  921. }
  922. /*
  923. * Write data to a file.
  924. * The stable flag requests synchronous writes.
  925. * N.B. After this call fhp needs an fh_put
  926. */
  927. __be32
  928. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  929. loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
  930. int *stablep)
  931. {
  932. __be32 err = 0;
  933. if (file) {
  934. err = nfsd_permission(fhp->fh_export, fhp->fh_dentry,
  935. MAY_WRITE|MAY_OWNER_OVERRIDE);
  936. if (err)
  937. goto out;
  938. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  939. stablep);
  940. } else {
  941. err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file);
  942. if (err)
  943. goto out;
  944. if (cnt)
  945. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  946. cnt, stablep);
  947. nfsd_close(file);
  948. }
  949. out:
  950. return err;
  951. }
  952. #ifdef CONFIG_NFSD_V3
  953. /*
  954. * Commit all pending writes to stable storage.
  955. * Strictly speaking, we could sync just the indicated file region here,
  956. * but there's currently no way we can ask the VFS to do so.
  957. *
  958. * Unfortunately we cannot lock the file to make sure we return full WCC
  959. * data to the client, as locking happens lower down in the filesystem.
  960. */
  961. __be32
  962. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  963. loff_t offset, unsigned long count)
  964. {
  965. struct file *file;
  966. __be32 err;
  967. if ((u64)count > ~(u64)offset)
  968. return nfserr_inval;
  969. if ((err = nfsd_open(rqstp, fhp, S_IFREG, MAY_WRITE, &file)) != 0)
  970. return err;
  971. if (EX_ISSYNC(fhp->fh_export)) {
  972. if (file->f_op && file->f_op->fsync) {
  973. err = nfserrno(nfsd_sync(file));
  974. } else {
  975. err = nfserr_notsupp;
  976. }
  977. }
  978. nfsd_close(file);
  979. return err;
  980. }
  981. #endif /* CONFIG_NFSD_V3 */
  982. /*
  983. * Create a file (regular, directory, device, fifo); UNIX sockets
  984. * not yet implemented.
  985. * If the response fh has been verified, the parent directory should
  986. * already be locked. Note that the parent directory is left locked.
  987. *
  988. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  989. */
  990. __be32
  991. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  992. char *fname, int flen, struct iattr *iap,
  993. int type, dev_t rdev, struct svc_fh *resfhp)
  994. {
  995. struct dentry *dentry, *dchild = NULL;
  996. struct inode *dirp;
  997. __be32 err;
  998. int host_err;
  999. err = nfserr_perm;
  1000. if (!flen)
  1001. goto out;
  1002. err = nfserr_exist;
  1003. if (isdotent(fname, flen))
  1004. goto out;
  1005. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1006. if (err)
  1007. goto out;
  1008. dentry = fhp->fh_dentry;
  1009. dirp = dentry->d_inode;
  1010. err = nfserr_notdir;
  1011. if(!dirp->i_op || !dirp->i_op->lookup)
  1012. goto out;
  1013. /*
  1014. * Check whether the response file handle has been verified yet.
  1015. * If it has, the parent directory should already be locked.
  1016. */
  1017. if (!resfhp->fh_dentry) {
  1018. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1019. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1020. dchild = lookup_one_len(fname, dentry, flen);
  1021. host_err = PTR_ERR(dchild);
  1022. if (IS_ERR(dchild))
  1023. goto out_nfserr;
  1024. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1025. if (err)
  1026. goto out;
  1027. } else {
  1028. /* called from nfsd_proc_create */
  1029. dchild = dget(resfhp->fh_dentry);
  1030. if (!fhp->fh_locked) {
  1031. /* not actually possible */
  1032. printk(KERN_ERR
  1033. "nfsd_create: parent %s/%s not locked!\n",
  1034. dentry->d_parent->d_name.name,
  1035. dentry->d_name.name);
  1036. err = nfserr_io;
  1037. goto out;
  1038. }
  1039. }
  1040. /*
  1041. * Make sure the child dentry is still negative ...
  1042. */
  1043. err = nfserr_exist;
  1044. if (dchild->d_inode) {
  1045. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1046. dentry->d_name.name, dchild->d_name.name);
  1047. goto out;
  1048. }
  1049. if (!(iap->ia_valid & ATTR_MODE))
  1050. iap->ia_mode = 0;
  1051. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1052. /*
  1053. * Get the dir op function pointer.
  1054. */
  1055. err = 0;
  1056. switch (type) {
  1057. case S_IFREG:
  1058. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1059. break;
  1060. case S_IFDIR:
  1061. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1062. break;
  1063. case S_IFCHR:
  1064. case S_IFBLK:
  1065. case S_IFIFO:
  1066. case S_IFSOCK:
  1067. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1068. break;
  1069. default:
  1070. printk("nfsd: bad file type %o in nfsd_create\n", type);
  1071. host_err = -EINVAL;
  1072. }
  1073. if (host_err < 0)
  1074. goto out_nfserr;
  1075. if (EX_ISSYNC(fhp->fh_export)) {
  1076. err = nfserrno(nfsd_sync_dir(dentry));
  1077. write_inode_now(dchild->d_inode, 1);
  1078. }
  1079. /* Set file attributes. Mode has already been set and
  1080. * setting uid/gid works only for root. Irix appears to
  1081. * send along the gid when it tries to implement setgid
  1082. * directories via NFS.
  1083. */
  1084. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1085. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1086. if (err2)
  1087. err = err2;
  1088. }
  1089. /*
  1090. * Update the file handle to get the new inode info.
  1091. */
  1092. if (!err)
  1093. err = fh_update(resfhp);
  1094. out:
  1095. if (dchild && !IS_ERR(dchild))
  1096. dput(dchild);
  1097. return err;
  1098. out_nfserr:
  1099. err = nfserrno(host_err);
  1100. goto out;
  1101. }
  1102. #ifdef CONFIG_NFSD_V3
  1103. /*
  1104. * NFSv3 version of nfsd_create
  1105. */
  1106. __be32
  1107. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1108. char *fname, int flen, struct iattr *iap,
  1109. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1110. int *truncp, int *created)
  1111. {
  1112. struct dentry *dentry, *dchild = NULL;
  1113. struct inode *dirp;
  1114. __be32 err;
  1115. int host_err;
  1116. __u32 v_mtime=0, v_atime=0;
  1117. err = nfserr_perm;
  1118. if (!flen)
  1119. goto out;
  1120. err = nfserr_exist;
  1121. if (isdotent(fname, flen))
  1122. goto out;
  1123. if (!(iap->ia_valid & ATTR_MODE))
  1124. iap->ia_mode = 0;
  1125. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1126. if (err)
  1127. goto out;
  1128. dentry = fhp->fh_dentry;
  1129. dirp = dentry->d_inode;
  1130. /* Get all the sanity checks out of the way before
  1131. * we lock the parent. */
  1132. err = nfserr_notdir;
  1133. if(!dirp->i_op || !dirp->i_op->lookup)
  1134. goto out;
  1135. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1136. /*
  1137. * Compose the response file handle.
  1138. */
  1139. dchild = lookup_one_len(fname, dentry, flen);
  1140. host_err = PTR_ERR(dchild);
  1141. if (IS_ERR(dchild))
  1142. goto out_nfserr;
  1143. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1144. if (err)
  1145. goto out;
  1146. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1147. /* solaris7 gets confused (bugid 4218508) if these have
  1148. * the high bit set, so just clear the high bits.
  1149. */
  1150. v_mtime = verifier[0]&0x7fffffff;
  1151. v_atime = verifier[1]&0x7fffffff;
  1152. }
  1153. if (dchild->d_inode) {
  1154. err = 0;
  1155. switch (createmode) {
  1156. case NFS3_CREATE_UNCHECKED:
  1157. if (! S_ISREG(dchild->d_inode->i_mode))
  1158. err = nfserr_exist;
  1159. else if (truncp) {
  1160. /* in nfsv4, we need to treat this case a little
  1161. * differently. we don't want to truncate the
  1162. * file now; this would be wrong if the OPEN
  1163. * fails for some other reason. furthermore,
  1164. * if the size is nonzero, we should ignore it
  1165. * according to spec!
  1166. */
  1167. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1168. }
  1169. else {
  1170. iap->ia_valid &= ATTR_SIZE;
  1171. goto set_attr;
  1172. }
  1173. break;
  1174. case NFS3_CREATE_EXCLUSIVE:
  1175. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1176. && dchild->d_inode->i_atime.tv_sec == v_atime
  1177. && dchild->d_inode->i_size == 0 )
  1178. break;
  1179. /* fallthru */
  1180. case NFS3_CREATE_GUARDED:
  1181. err = nfserr_exist;
  1182. }
  1183. goto out;
  1184. }
  1185. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1186. if (host_err < 0)
  1187. goto out_nfserr;
  1188. if (created)
  1189. *created = 1;
  1190. if (EX_ISSYNC(fhp->fh_export)) {
  1191. err = nfserrno(nfsd_sync_dir(dentry));
  1192. /* setattr will sync the child (or not) */
  1193. }
  1194. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1195. /* Cram the verifier into atime/mtime */
  1196. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1197. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1198. /* XXX someone who knows this better please fix it for nsec */
  1199. iap->ia_mtime.tv_sec = v_mtime;
  1200. iap->ia_atime.tv_sec = v_atime;
  1201. iap->ia_mtime.tv_nsec = 0;
  1202. iap->ia_atime.tv_nsec = 0;
  1203. }
  1204. /* Set file attributes.
  1205. * Irix appears to send along the gid when it tries to
  1206. * implement setgid directories via NFS. Clear out all that cruft.
  1207. */
  1208. set_attr:
  1209. if ((iap->ia_valid &= ~(ATTR_UID|ATTR_GID|ATTR_MODE)) != 0) {
  1210. __be32 err2 = nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1211. if (err2)
  1212. err = err2;
  1213. }
  1214. /*
  1215. * Update the filehandle to get the new inode info.
  1216. */
  1217. if (!err)
  1218. err = fh_update(resfhp);
  1219. out:
  1220. fh_unlock(fhp);
  1221. if (dchild && !IS_ERR(dchild))
  1222. dput(dchild);
  1223. return err;
  1224. out_nfserr:
  1225. err = nfserrno(host_err);
  1226. goto out;
  1227. }
  1228. #endif /* CONFIG_NFSD_V3 */
  1229. /*
  1230. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1231. * fits into the buffer. On return, it contains the true length.
  1232. * N.B. After this call fhp needs an fh_put
  1233. */
  1234. __be32
  1235. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1236. {
  1237. struct dentry *dentry;
  1238. struct inode *inode;
  1239. mm_segment_t oldfs;
  1240. __be32 err;
  1241. int host_err;
  1242. err = fh_verify(rqstp, fhp, S_IFLNK, MAY_NOP);
  1243. if (err)
  1244. goto out;
  1245. dentry = fhp->fh_dentry;
  1246. inode = dentry->d_inode;
  1247. err = nfserr_inval;
  1248. if (!inode->i_op || !inode->i_op->readlink)
  1249. goto out;
  1250. touch_atime(fhp->fh_export->ex_mnt, dentry);
  1251. /* N.B. Why does this call need a get_fs()??
  1252. * Remove the set_fs and watch the fireworks:-) --okir
  1253. */
  1254. oldfs = get_fs(); set_fs(KERNEL_DS);
  1255. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1256. set_fs(oldfs);
  1257. if (host_err < 0)
  1258. goto out_nfserr;
  1259. *lenp = host_err;
  1260. err = 0;
  1261. out:
  1262. return err;
  1263. out_nfserr:
  1264. err = nfserrno(host_err);
  1265. goto out;
  1266. }
  1267. /*
  1268. * Create a symlink and look up its inode
  1269. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1270. */
  1271. __be32
  1272. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1273. char *fname, int flen,
  1274. char *path, int plen,
  1275. struct svc_fh *resfhp,
  1276. struct iattr *iap)
  1277. {
  1278. struct dentry *dentry, *dnew;
  1279. __be32 err, cerr;
  1280. int host_err;
  1281. umode_t mode;
  1282. err = nfserr_noent;
  1283. if (!flen || !plen)
  1284. goto out;
  1285. err = nfserr_exist;
  1286. if (isdotent(fname, flen))
  1287. goto out;
  1288. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_CREATE);
  1289. if (err)
  1290. goto out;
  1291. fh_lock(fhp);
  1292. dentry = fhp->fh_dentry;
  1293. dnew = lookup_one_len(fname, dentry, flen);
  1294. host_err = PTR_ERR(dnew);
  1295. if (IS_ERR(dnew))
  1296. goto out_nfserr;
  1297. mode = S_IALLUGO;
  1298. /* Only the MODE ATTRibute is even vaguely meaningful */
  1299. if (iap && (iap->ia_valid & ATTR_MODE))
  1300. mode = iap->ia_mode & S_IALLUGO;
  1301. if (unlikely(path[plen] != 0)) {
  1302. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1303. if (path_alloced == NULL)
  1304. host_err = -ENOMEM;
  1305. else {
  1306. strncpy(path_alloced, path, plen);
  1307. path_alloced[plen] = 0;
  1308. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced, mode);
  1309. kfree(path_alloced);
  1310. }
  1311. } else
  1312. host_err = vfs_symlink(dentry->d_inode, dnew, path, mode);
  1313. if (!host_err) {
  1314. if (EX_ISSYNC(fhp->fh_export))
  1315. host_err = nfsd_sync_dir(dentry);
  1316. }
  1317. err = nfserrno(host_err);
  1318. fh_unlock(fhp);
  1319. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1320. dput(dnew);
  1321. if (err==0) err = cerr;
  1322. out:
  1323. return err;
  1324. out_nfserr:
  1325. err = nfserrno(host_err);
  1326. goto out;
  1327. }
  1328. /*
  1329. * Create a hardlink
  1330. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1331. */
  1332. __be32
  1333. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1334. char *name, int len, struct svc_fh *tfhp)
  1335. {
  1336. struct dentry *ddir, *dnew, *dold;
  1337. struct inode *dirp, *dest;
  1338. __be32 err;
  1339. int host_err;
  1340. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_CREATE);
  1341. if (err)
  1342. goto out;
  1343. err = fh_verify(rqstp, tfhp, -S_IFDIR, MAY_NOP);
  1344. if (err)
  1345. goto out;
  1346. err = nfserr_perm;
  1347. if (!len)
  1348. goto out;
  1349. err = nfserr_exist;
  1350. if (isdotent(name, len))
  1351. goto out;
  1352. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1353. ddir = ffhp->fh_dentry;
  1354. dirp = ddir->d_inode;
  1355. dnew = lookup_one_len(name, ddir, len);
  1356. host_err = PTR_ERR(dnew);
  1357. if (IS_ERR(dnew))
  1358. goto out_nfserr;
  1359. dold = tfhp->fh_dentry;
  1360. dest = dold->d_inode;
  1361. host_err = vfs_link(dold, dirp, dnew);
  1362. if (!host_err) {
  1363. if (EX_ISSYNC(ffhp->fh_export)) {
  1364. err = nfserrno(nfsd_sync_dir(ddir));
  1365. write_inode_now(dest, 1);
  1366. }
  1367. err = 0;
  1368. } else {
  1369. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1370. err = nfserr_acces;
  1371. else
  1372. err = nfserrno(host_err);
  1373. }
  1374. dput(dnew);
  1375. out_unlock:
  1376. fh_unlock(ffhp);
  1377. out:
  1378. return err;
  1379. out_nfserr:
  1380. err = nfserrno(host_err);
  1381. goto out_unlock;
  1382. }
  1383. /*
  1384. * Rename a file
  1385. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1386. */
  1387. __be32
  1388. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1389. struct svc_fh *tfhp, char *tname, int tlen)
  1390. {
  1391. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1392. struct inode *fdir, *tdir;
  1393. __be32 err;
  1394. int host_err;
  1395. err = fh_verify(rqstp, ffhp, S_IFDIR, MAY_REMOVE);
  1396. if (err)
  1397. goto out;
  1398. err = fh_verify(rqstp, tfhp, S_IFDIR, MAY_CREATE);
  1399. if (err)
  1400. goto out;
  1401. fdentry = ffhp->fh_dentry;
  1402. fdir = fdentry->d_inode;
  1403. tdentry = tfhp->fh_dentry;
  1404. tdir = tdentry->d_inode;
  1405. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1406. if (ffhp->fh_export != tfhp->fh_export)
  1407. goto out;
  1408. err = nfserr_perm;
  1409. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1410. goto out;
  1411. /* cannot use fh_lock as we need deadlock protective ordering
  1412. * so do it by hand */
  1413. trap = lock_rename(tdentry, fdentry);
  1414. ffhp->fh_locked = tfhp->fh_locked = 1;
  1415. fill_pre_wcc(ffhp);
  1416. fill_pre_wcc(tfhp);
  1417. odentry = lookup_one_len(fname, fdentry, flen);
  1418. host_err = PTR_ERR(odentry);
  1419. if (IS_ERR(odentry))
  1420. goto out_nfserr;
  1421. host_err = -ENOENT;
  1422. if (!odentry->d_inode)
  1423. goto out_dput_old;
  1424. host_err = -EINVAL;
  1425. if (odentry == trap)
  1426. goto out_dput_old;
  1427. ndentry = lookup_one_len(tname, tdentry, tlen);
  1428. host_err = PTR_ERR(ndentry);
  1429. if (IS_ERR(ndentry))
  1430. goto out_dput_old;
  1431. host_err = -ENOTEMPTY;
  1432. if (ndentry == trap)
  1433. goto out_dput_new;
  1434. #ifdef MSNFS
  1435. if ((ffhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1436. ((atomic_read(&odentry->d_count) > 1)
  1437. || (atomic_read(&ndentry->d_count) > 1))) {
  1438. host_err = -EPERM;
  1439. } else
  1440. #endif
  1441. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1442. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1443. host_err = nfsd_sync_dir(tdentry);
  1444. if (!host_err)
  1445. host_err = nfsd_sync_dir(fdentry);
  1446. }
  1447. out_dput_new:
  1448. dput(ndentry);
  1449. out_dput_old:
  1450. dput(odentry);
  1451. out_nfserr:
  1452. err = nfserrno(host_err);
  1453. /* we cannot reply on fh_unlock on the two filehandles,
  1454. * as that would do the wrong thing if the two directories
  1455. * were the same, so again we do it by hand
  1456. */
  1457. fill_post_wcc(ffhp);
  1458. fill_post_wcc(tfhp);
  1459. unlock_rename(tdentry, fdentry);
  1460. ffhp->fh_locked = tfhp->fh_locked = 0;
  1461. out:
  1462. return err;
  1463. }
  1464. /*
  1465. * Unlink a file or directory
  1466. * N.B. After this call fhp needs an fh_put
  1467. */
  1468. __be32
  1469. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1470. char *fname, int flen)
  1471. {
  1472. struct dentry *dentry, *rdentry;
  1473. struct inode *dirp;
  1474. __be32 err;
  1475. int host_err;
  1476. err = nfserr_acces;
  1477. if (!flen || isdotent(fname, flen))
  1478. goto out;
  1479. err = fh_verify(rqstp, fhp, S_IFDIR, MAY_REMOVE);
  1480. if (err)
  1481. goto out;
  1482. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1483. dentry = fhp->fh_dentry;
  1484. dirp = dentry->d_inode;
  1485. rdentry = lookup_one_len(fname, dentry, flen);
  1486. host_err = PTR_ERR(rdentry);
  1487. if (IS_ERR(rdentry))
  1488. goto out_nfserr;
  1489. if (!rdentry->d_inode) {
  1490. dput(rdentry);
  1491. err = nfserr_noent;
  1492. goto out;
  1493. }
  1494. if (!type)
  1495. type = rdentry->d_inode->i_mode & S_IFMT;
  1496. if (type != S_IFDIR) { /* It's UNLINK */
  1497. #ifdef MSNFS
  1498. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1499. (atomic_read(&rdentry->d_count) > 1)) {
  1500. host_err = -EPERM;
  1501. } else
  1502. #endif
  1503. host_err = vfs_unlink(dirp, rdentry);
  1504. } else { /* It's RMDIR */
  1505. host_err = vfs_rmdir(dirp, rdentry);
  1506. }
  1507. dput(rdentry);
  1508. if (host_err)
  1509. goto out_nfserr;
  1510. if (EX_ISSYNC(fhp->fh_export))
  1511. host_err = nfsd_sync_dir(dentry);
  1512. out_nfserr:
  1513. err = nfserrno(host_err);
  1514. out:
  1515. return err;
  1516. }
  1517. /*
  1518. * Read entries from a directory.
  1519. * The NFSv3/4 verifier we ignore for now.
  1520. */
  1521. __be32
  1522. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1523. struct readdir_cd *cdp, filldir_t func)
  1524. {
  1525. __be32 err;
  1526. int host_err;
  1527. struct file *file;
  1528. loff_t offset = *offsetp;
  1529. err = nfsd_open(rqstp, fhp, S_IFDIR, MAY_READ, &file);
  1530. if (err)
  1531. goto out;
  1532. offset = vfs_llseek(file, offset, 0);
  1533. if (offset < 0) {
  1534. err = nfserrno((int)offset);
  1535. goto out_close;
  1536. }
  1537. /*
  1538. * Read the directory entries. This silly loop is necessary because
  1539. * readdir() is not guaranteed to fill up the entire buffer, but
  1540. * may choose to do less.
  1541. */
  1542. do {
  1543. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1544. host_err = vfs_readdir(file, func, cdp);
  1545. } while (host_err >=0 && cdp->err == nfs_ok);
  1546. if (host_err)
  1547. err = nfserrno(host_err);
  1548. else
  1549. err = cdp->err;
  1550. *offsetp = vfs_llseek(file, 0, 1);
  1551. if (err == nfserr_eof || err == nfserr_toosmall)
  1552. err = nfs_ok; /* can still be found in ->err */
  1553. out_close:
  1554. nfsd_close(file);
  1555. out:
  1556. return err;
  1557. }
  1558. /*
  1559. * Get file system stats
  1560. * N.B. After this call fhp needs an fh_put
  1561. */
  1562. __be32
  1563. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat)
  1564. {
  1565. __be32 err = fh_verify(rqstp, fhp, 0, MAY_NOP);
  1566. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1567. err = nfserr_io;
  1568. return err;
  1569. }
  1570. /*
  1571. * Check for a user's access permissions to this inode.
  1572. */
  1573. __be32
  1574. nfsd_permission(struct svc_export *exp, struct dentry *dentry, int acc)
  1575. {
  1576. struct inode *inode = dentry->d_inode;
  1577. int err;
  1578. if (acc == MAY_NOP)
  1579. return 0;
  1580. #if 0
  1581. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1582. acc,
  1583. (acc & MAY_READ)? " read" : "",
  1584. (acc & MAY_WRITE)? " write" : "",
  1585. (acc & MAY_EXEC)? " exec" : "",
  1586. (acc & MAY_SATTR)? " sattr" : "",
  1587. (acc & MAY_TRUNC)? " trunc" : "",
  1588. (acc & MAY_LOCK)? " lock" : "",
  1589. (acc & MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1590. inode->i_mode,
  1591. IS_IMMUTABLE(inode)? " immut" : "",
  1592. IS_APPEND(inode)? " append" : "",
  1593. IS_RDONLY(inode)? " ro" : "");
  1594. dprintk(" owner %d/%d user %d/%d\n",
  1595. inode->i_uid, inode->i_gid, current->fsuid, current->fsgid);
  1596. #endif
  1597. /* Normally we reject any write/sattr etc access on a read-only file
  1598. * system. But if it is IRIX doing check on write-access for a
  1599. * device special file, we ignore rofs.
  1600. */
  1601. if (!(acc & MAY_LOCAL_ACCESS))
  1602. if (acc & (MAY_WRITE | MAY_SATTR | MAY_TRUNC)) {
  1603. if (EX_RDONLY(exp) || IS_RDONLY(inode))
  1604. return nfserr_rofs;
  1605. if (/* (acc & MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1606. return nfserr_perm;
  1607. }
  1608. if ((acc & MAY_TRUNC) && IS_APPEND(inode))
  1609. return nfserr_perm;
  1610. if (acc & MAY_LOCK) {
  1611. /* If we cannot rely on authentication in NLM requests,
  1612. * just allow locks, otherwise require read permission, or
  1613. * ownership
  1614. */
  1615. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1616. return 0;
  1617. else
  1618. acc = MAY_READ | MAY_OWNER_OVERRIDE;
  1619. }
  1620. /*
  1621. * The file owner always gets access permission for accesses that
  1622. * would normally be checked at open time. This is to make
  1623. * file access work even when the client has done a fchmod(fd, 0).
  1624. *
  1625. * However, `cp foo bar' should fail nevertheless when bar is
  1626. * readonly. A sensible way to do this might be to reject all
  1627. * attempts to truncate a read-only file, because a creat() call
  1628. * always implies file truncation.
  1629. * ... but this isn't really fair. A process may reasonably call
  1630. * ftruncate on an open file descriptor on a file with perm 000.
  1631. * We must trust the client to do permission checking - using "ACCESS"
  1632. * with NFSv3.
  1633. */
  1634. if ((acc & MAY_OWNER_OVERRIDE) &&
  1635. inode->i_uid == current->fsuid)
  1636. return 0;
  1637. err = permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC), NULL);
  1638. /* Allow read access to binaries even when mode 111 */
  1639. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1640. acc == (MAY_READ | MAY_OWNER_OVERRIDE))
  1641. err = permission(inode, MAY_EXEC, NULL);
  1642. return err? nfserrno(err) : 0;
  1643. }
  1644. void
  1645. nfsd_racache_shutdown(void)
  1646. {
  1647. if (!raparml)
  1648. return;
  1649. dprintk("nfsd: freeing readahead buffers.\n");
  1650. kfree(raparml);
  1651. raparml = NULL;
  1652. }
  1653. /*
  1654. * Initialize readahead param cache
  1655. */
  1656. int
  1657. nfsd_racache_init(int cache_size)
  1658. {
  1659. int i;
  1660. int j = 0;
  1661. int nperbucket;
  1662. if (raparml)
  1663. return 0;
  1664. if (cache_size < 2*RAPARM_HASH_SIZE)
  1665. cache_size = 2*RAPARM_HASH_SIZE;
  1666. raparml = kcalloc(cache_size, sizeof(struct raparms), GFP_KERNEL);
  1667. if (!raparml) {
  1668. printk(KERN_WARNING
  1669. "nfsd: Could not allocate memory read-ahead cache.\n");
  1670. return -ENOMEM;
  1671. }
  1672. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1673. for (i = 0 ; i < RAPARM_HASH_SIZE ; i++) {
  1674. raparm_hash[i].pb_head = NULL;
  1675. spin_lock_init(&raparm_hash[i].pb_lock);
  1676. }
  1677. nperbucket = cache_size >> RAPARM_HASH_BITS;
  1678. for (i = 0; i < cache_size - 1; i++) {
  1679. if (i % nperbucket == 0)
  1680. raparm_hash[j++].pb_head = raparml + i;
  1681. if (i % nperbucket < nperbucket-1)
  1682. raparml[i].p_next = raparml + i + 1;
  1683. }
  1684. nfsdstats.ra_size = cache_size;
  1685. return 0;
  1686. }
  1687. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1688. struct posix_acl *
  1689. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1690. {
  1691. struct inode *inode = fhp->fh_dentry->d_inode;
  1692. char *name;
  1693. void *value = NULL;
  1694. ssize_t size;
  1695. struct posix_acl *acl;
  1696. if (!IS_POSIXACL(inode))
  1697. return ERR_PTR(-EOPNOTSUPP);
  1698. switch (type) {
  1699. case ACL_TYPE_ACCESS:
  1700. name = POSIX_ACL_XATTR_ACCESS;
  1701. break;
  1702. case ACL_TYPE_DEFAULT:
  1703. name = POSIX_ACL_XATTR_DEFAULT;
  1704. break;
  1705. default:
  1706. return ERR_PTR(-EOPNOTSUPP);
  1707. }
  1708. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1709. if (size < 0)
  1710. return ERR_PTR(size);
  1711. acl = posix_acl_from_xattr(value, size);
  1712. kfree(value);
  1713. return acl;
  1714. }
  1715. int
  1716. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1717. {
  1718. struct inode *inode = fhp->fh_dentry->d_inode;
  1719. char *name;
  1720. void *value = NULL;
  1721. size_t size;
  1722. int error;
  1723. if (!IS_POSIXACL(inode) || !inode->i_op ||
  1724. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1725. return -EOPNOTSUPP;
  1726. switch(type) {
  1727. case ACL_TYPE_ACCESS:
  1728. name = POSIX_ACL_XATTR_ACCESS;
  1729. break;
  1730. case ACL_TYPE_DEFAULT:
  1731. name = POSIX_ACL_XATTR_DEFAULT;
  1732. break;
  1733. default:
  1734. return -EOPNOTSUPP;
  1735. }
  1736. if (acl && acl->a_count) {
  1737. size = posix_acl_xattr_size(acl->a_count);
  1738. value = kmalloc(size, GFP_KERNEL);
  1739. if (!value)
  1740. return -ENOMEM;
  1741. error = posix_acl_to_xattr(acl, value, size);
  1742. if (error < 0)
  1743. goto getout;
  1744. size = error;
  1745. } else
  1746. size = 0;
  1747. if (size)
  1748. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1749. else {
  1750. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1751. error = 0;
  1752. else {
  1753. error = vfs_removexattr(fhp->fh_dentry, name);
  1754. if (error == -ENODATA)
  1755. error = 0;
  1756. }
  1757. }
  1758. getout:
  1759. kfree(value);
  1760. return error;
  1761. }
  1762. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */