vfs.c 52 KB

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