vfs.c 53 KB

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