vfs.c 49 KB

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