vfs.c 54 KB

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