vfs.c 47 KB

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