vfs.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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_fdatawrite(inode->i_mapping);
  687. if (err == 0 && fop && (fsync = fop->fsync))
  688. err = fsync(filp, dp, 0);
  689. if (err == 0)
  690. err = filemap_fdatawait(inode->i_mapping);
  691. return err;
  692. }
  693. static int
  694. nfsd_sync(struct file *filp)
  695. {
  696. int err;
  697. struct inode *inode = filp->f_path.dentry->d_inode;
  698. dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
  699. mutex_lock(&inode->i_mutex);
  700. err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
  701. mutex_unlock(&inode->i_mutex);
  702. return err;
  703. }
  704. int
  705. nfsd_sync_dir(struct dentry *dp)
  706. {
  707. return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
  708. }
  709. /*
  710. * Obtain the readahead parameters for the file
  711. * specified by (dev, ino).
  712. */
  713. static inline struct raparms *
  714. nfsd_get_raparms(dev_t dev, ino_t ino)
  715. {
  716. struct raparms *ra, **rap, **frap = NULL;
  717. int depth = 0;
  718. unsigned int hash;
  719. struct raparm_hbucket *rab;
  720. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  721. rab = &raparm_hash[hash];
  722. spin_lock(&rab->pb_lock);
  723. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  724. if (ra->p_ino == ino && ra->p_dev == dev)
  725. goto found;
  726. depth++;
  727. if (ra->p_count == 0)
  728. frap = rap;
  729. }
  730. depth = nfsdstats.ra_size*11/10;
  731. if (!frap) {
  732. spin_unlock(&rab->pb_lock);
  733. return NULL;
  734. }
  735. rap = frap;
  736. ra = *frap;
  737. ra->p_dev = dev;
  738. ra->p_ino = ino;
  739. ra->p_set = 0;
  740. ra->p_hindex = hash;
  741. found:
  742. if (rap != &rab->pb_head) {
  743. *rap = ra->p_next;
  744. ra->p_next = rab->pb_head;
  745. rab->pb_head = ra;
  746. }
  747. ra->p_count++;
  748. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  749. spin_unlock(&rab->pb_lock);
  750. return ra;
  751. }
  752. /*
  753. * Grab and keep cached pages associated with a file in the svc_rqst
  754. * so that they can be passed to the network sendmsg/sendpage routines
  755. * directly. They will be released after the sending has completed.
  756. */
  757. static int
  758. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  759. struct splice_desc *sd)
  760. {
  761. struct svc_rqst *rqstp = sd->u.data;
  762. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  763. struct page *page = buf->page;
  764. size_t size;
  765. int ret;
  766. ret = buf->ops->confirm(pipe, buf);
  767. if (unlikely(ret))
  768. return ret;
  769. size = sd->len;
  770. if (rqstp->rq_res.page_len == 0) {
  771. get_page(page);
  772. put_page(*pp);
  773. *pp = page;
  774. rqstp->rq_resused++;
  775. rqstp->rq_res.page_base = buf->offset;
  776. rqstp->rq_res.page_len = size;
  777. } else if (page != pp[-1]) {
  778. get_page(page);
  779. if (*pp)
  780. put_page(*pp);
  781. *pp = page;
  782. rqstp->rq_resused++;
  783. rqstp->rq_res.page_len += size;
  784. } else
  785. rqstp->rq_res.page_len += size;
  786. return size;
  787. }
  788. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  789. struct splice_desc *sd)
  790. {
  791. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  792. }
  793. static inline int svc_msnfs(struct svc_fh *ffhp)
  794. {
  795. #ifdef MSNFS
  796. return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
  797. #else
  798. return 0;
  799. #endif
  800. }
  801. static __be32
  802. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  803. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  804. {
  805. struct inode *inode;
  806. struct raparms *ra;
  807. mm_segment_t oldfs;
  808. __be32 err;
  809. int host_err;
  810. err = nfserr_perm;
  811. inode = file->f_path.dentry->d_inode;
  812. if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
  813. goto out;
  814. /* Get readahead parameters */
  815. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  816. if (ra && ra->p_set)
  817. file->f_ra = ra->p_ra;
  818. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  819. struct splice_desc sd = {
  820. .len = 0,
  821. .total_len = *count,
  822. .pos = offset,
  823. .u.data = rqstp,
  824. };
  825. rqstp->rq_resused = 1;
  826. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  827. } else {
  828. oldfs = get_fs();
  829. set_fs(KERNEL_DS);
  830. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  831. set_fs(oldfs);
  832. }
  833. /* Write back readahead params */
  834. if (ra) {
  835. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  836. spin_lock(&rab->pb_lock);
  837. ra->p_ra = file->f_ra;
  838. ra->p_set = 1;
  839. ra->p_count--;
  840. spin_unlock(&rab->pb_lock);
  841. }
  842. if (host_err >= 0) {
  843. nfsdstats.io_read += host_err;
  844. *count = host_err;
  845. err = 0;
  846. fsnotify_access(file->f_path.dentry);
  847. } else
  848. err = nfserrno(host_err);
  849. out:
  850. return err;
  851. }
  852. static void kill_suid(struct dentry *dentry)
  853. {
  854. struct iattr ia;
  855. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  856. mutex_lock(&dentry->d_inode->i_mutex);
  857. notify_change(dentry, &ia);
  858. mutex_unlock(&dentry->d_inode->i_mutex);
  859. }
  860. /*
  861. * Gathered writes: If another process is currently writing to the file,
  862. * there's a high chance this is another nfsd (triggered by a bulk write
  863. * from a client's biod). Rather than syncing the file with each write
  864. * request, we sleep for 10 msec.
  865. *
  866. * I don't know if this roughly approximates C. Juszak's idea of
  867. * gathered writes, but it's a nice and simple solution (IMHO), and it
  868. * seems to work:-)
  869. *
  870. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  871. * better tool (separate unstable writes and commits) for solving this
  872. * problem.
  873. */
  874. static int wait_for_concurrent_writes(struct file *file)
  875. {
  876. struct inode *inode = file->f_path.dentry->d_inode;
  877. static ino_t last_ino;
  878. static dev_t last_dev;
  879. int err = 0;
  880. if (atomic_read(&inode->i_writecount) > 1
  881. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  882. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  883. msleep(10);
  884. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  885. }
  886. if (inode->i_state & I_DIRTY) {
  887. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  888. err = nfsd_sync(file);
  889. }
  890. last_ino = inode->i_ino;
  891. last_dev = inode->i_sb->s_dev;
  892. return err;
  893. }
  894. static __be32
  895. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  896. loff_t offset, struct kvec *vec, int vlen,
  897. unsigned long *cnt, int *stablep)
  898. {
  899. struct svc_export *exp;
  900. struct dentry *dentry;
  901. struct inode *inode;
  902. mm_segment_t oldfs;
  903. __be32 err = 0;
  904. int host_err;
  905. int stable = *stablep;
  906. int use_wgather;
  907. #ifdef MSNFS
  908. err = nfserr_perm;
  909. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  910. (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
  911. goto out;
  912. #endif
  913. dentry = file->f_path.dentry;
  914. inode = dentry->d_inode;
  915. exp = fhp->fh_export;
  916. /*
  917. * Request sync writes if
  918. * - the sync export option has been set, or
  919. * - the client requested O_SYNC behavior (NFSv3 feature).
  920. * - The file system doesn't support fsync().
  921. * When NFSv2 gathered writes have been configured for this volume,
  922. * flushing the data to disk is handled separately below.
  923. */
  924. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  925. if (!file->f_op->fsync) {/* COMMIT3 cannot work */
  926. stable = 2;
  927. *stablep = 2; /* FILE_SYNC */
  928. }
  929. if (!EX_ISSYNC(exp))
  930. stable = 0;
  931. if (stable && !use_wgather) {
  932. spin_lock(&file->f_lock);
  933. file->f_flags |= O_SYNC;
  934. spin_unlock(&file->f_lock);
  935. }
  936. /* Write the data. */
  937. oldfs = get_fs(); set_fs(KERNEL_DS);
  938. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  939. set_fs(oldfs);
  940. if (host_err < 0)
  941. goto out_nfserr;
  942. *cnt = host_err;
  943. nfsdstats.io_write += host_err;
  944. fsnotify_modify(file->f_path.dentry);
  945. /* clear setuid/setgid flag after write */
  946. if (inode->i_mode & (S_ISUID | S_ISGID))
  947. kill_suid(dentry);
  948. if (stable && use_wgather)
  949. host_err = wait_for_concurrent_writes(file);
  950. out_nfserr:
  951. dprintk("nfsd: write complete host_err=%d\n", host_err);
  952. if (host_err >= 0)
  953. err = 0;
  954. else
  955. err = nfserrno(host_err);
  956. out:
  957. return err;
  958. }
  959. /*
  960. * Read data from a file. count must contain the requested read count
  961. * on entry. On return, *count contains the number of bytes actually read.
  962. * N.B. After this call fhp needs an fh_put
  963. */
  964. __be32
  965. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  966. loff_t offset, struct kvec *vec, int vlen,
  967. unsigned long *count)
  968. {
  969. __be32 err;
  970. if (file) {
  971. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  972. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  973. if (err)
  974. goto out;
  975. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  976. } else {
  977. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  978. if (err)
  979. goto out;
  980. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  981. nfsd_close(file);
  982. }
  983. out:
  984. return err;
  985. }
  986. /*
  987. * Write data to a file.
  988. * The stable flag requests synchronous writes.
  989. * N.B. After this call fhp needs an fh_put
  990. */
  991. __be32
  992. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  993. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  994. int *stablep)
  995. {
  996. __be32 err = 0;
  997. if (file) {
  998. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  999. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  1000. if (err)
  1001. goto out;
  1002. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  1003. stablep);
  1004. } else {
  1005. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  1006. if (err)
  1007. goto out;
  1008. if (cnt)
  1009. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  1010. cnt, stablep);
  1011. nfsd_close(file);
  1012. }
  1013. out:
  1014. return err;
  1015. }
  1016. #ifdef CONFIG_NFSD_V3
  1017. /*
  1018. * Commit all pending writes to stable storage.
  1019. * Strictly speaking, we could sync just the indicated file region here,
  1020. * but there's currently no way we can ask the VFS to do so.
  1021. *
  1022. * Unfortunately we cannot lock the file to make sure we return full WCC
  1023. * data to the client, as locking happens lower down in the filesystem.
  1024. */
  1025. __be32
  1026. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1027. loff_t offset, unsigned long count)
  1028. {
  1029. struct file *file;
  1030. __be32 err;
  1031. if ((u64)count > ~(u64)offset)
  1032. return nfserr_inval;
  1033. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  1034. if (err)
  1035. return err;
  1036. if (EX_ISSYNC(fhp->fh_export)) {
  1037. if (file->f_op && file->f_op->fsync) {
  1038. err = nfserrno(nfsd_sync(file));
  1039. } else {
  1040. err = nfserr_notsupp;
  1041. }
  1042. }
  1043. nfsd_close(file);
  1044. return err;
  1045. }
  1046. #endif /* CONFIG_NFSD_V3 */
  1047. static __be32
  1048. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1049. struct iattr *iap)
  1050. {
  1051. /*
  1052. * Mode has already been set earlier in create:
  1053. */
  1054. iap->ia_valid &= ~ATTR_MODE;
  1055. /*
  1056. * Setting uid/gid works only for root. Irix appears to
  1057. * send along the gid on create when it tries to implement
  1058. * setgid directories via NFS:
  1059. */
  1060. if (current_fsuid() != 0)
  1061. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1062. if (iap->ia_valid)
  1063. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1064. return 0;
  1065. }
  1066. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1067. * setting size to 0 may fail for some specific file systems by the permission
  1068. * checking which requires WRITE permission but the mode is 000.
  1069. * we ignore the resizing(to 0) on the just new created file, since the size is
  1070. * 0 after file created.
  1071. *
  1072. * call this only after vfs_create() is called.
  1073. * */
  1074. static void
  1075. nfsd_check_ignore_resizing(struct iattr *iap)
  1076. {
  1077. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1078. iap->ia_valid &= ~ATTR_SIZE;
  1079. }
  1080. /*
  1081. * Create a file (regular, directory, device, fifo); UNIX sockets
  1082. * not yet implemented.
  1083. * If the response fh has been verified, the parent directory should
  1084. * already be locked. Note that the parent directory is left locked.
  1085. *
  1086. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1087. */
  1088. __be32
  1089. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1090. char *fname, int flen, struct iattr *iap,
  1091. int type, dev_t rdev, struct svc_fh *resfhp)
  1092. {
  1093. struct dentry *dentry, *dchild = NULL;
  1094. struct inode *dirp;
  1095. __be32 err;
  1096. __be32 err2;
  1097. int host_err;
  1098. err = nfserr_perm;
  1099. if (!flen)
  1100. goto out;
  1101. err = nfserr_exist;
  1102. if (isdotent(fname, flen))
  1103. goto out;
  1104. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1105. if (err)
  1106. goto out;
  1107. dentry = fhp->fh_dentry;
  1108. dirp = dentry->d_inode;
  1109. err = nfserr_notdir;
  1110. if (!dirp->i_op->lookup)
  1111. goto out;
  1112. /*
  1113. * Check whether the response file handle has been verified yet.
  1114. * If it has, the parent directory should already be locked.
  1115. */
  1116. if (!resfhp->fh_dentry) {
  1117. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1118. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1119. dchild = lookup_one_len(fname, dentry, flen);
  1120. host_err = PTR_ERR(dchild);
  1121. if (IS_ERR(dchild))
  1122. goto out_nfserr;
  1123. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1124. if (err)
  1125. goto out;
  1126. } else {
  1127. /* called from nfsd_proc_create */
  1128. dchild = dget(resfhp->fh_dentry);
  1129. if (!fhp->fh_locked) {
  1130. /* not actually possible */
  1131. printk(KERN_ERR
  1132. "nfsd_create: parent %s/%s not locked!\n",
  1133. dentry->d_parent->d_name.name,
  1134. dentry->d_name.name);
  1135. err = nfserr_io;
  1136. goto out;
  1137. }
  1138. }
  1139. /*
  1140. * Make sure the child dentry is still negative ...
  1141. */
  1142. err = nfserr_exist;
  1143. if (dchild->d_inode) {
  1144. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1145. dentry->d_name.name, dchild->d_name.name);
  1146. goto out;
  1147. }
  1148. if (!(iap->ia_valid & ATTR_MODE))
  1149. iap->ia_mode = 0;
  1150. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1151. err = nfserr_inval;
  1152. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1153. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1154. type);
  1155. goto out;
  1156. }
  1157. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1158. if (host_err)
  1159. goto out_nfserr;
  1160. /*
  1161. * Get the dir op function pointer.
  1162. */
  1163. err = 0;
  1164. switch (type) {
  1165. case S_IFREG:
  1166. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1167. if (!host_err)
  1168. nfsd_check_ignore_resizing(iap);
  1169. break;
  1170. case S_IFDIR:
  1171. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1172. break;
  1173. case S_IFCHR:
  1174. case S_IFBLK:
  1175. case S_IFIFO:
  1176. case S_IFSOCK:
  1177. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1178. break;
  1179. }
  1180. if (host_err < 0) {
  1181. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1182. goto out_nfserr;
  1183. }
  1184. if (EX_ISSYNC(fhp->fh_export)) {
  1185. err = nfserrno(nfsd_sync_dir(dentry));
  1186. write_inode_now(dchild->d_inode, 1);
  1187. }
  1188. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1189. if (err2)
  1190. err = err2;
  1191. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1192. /*
  1193. * Update the file handle to get the new inode info.
  1194. */
  1195. if (!err)
  1196. err = fh_update(resfhp);
  1197. out:
  1198. if (dchild && !IS_ERR(dchild))
  1199. dput(dchild);
  1200. return err;
  1201. out_nfserr:
  1202. err = nfserrno(host_err);
  1203. goto out;
  1204. }
  1205. #ifdef CONFIG_NFSD_V3
  1206. /*
  1207. * NFSv3 version of nfsd_create
  1208. */
  1209. __be32
  1210. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1211. char *fname, int flen, struct iattr *iap,
  1212. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1213. int *truncp, int *created)
  1214. {
  1215. struct dentry *dentry, *dchild = NULL;
  1216. struct inode *dirp;
  1217. __be32 err;
  1218. __be32 err2;
  1219. int host_err;
  1220. __u32 v_mtime=0, v_atime=0;
  1221. err = nfserr_perm;
  1222. if (!flen)
  1223. goto out;
  1224. err = nfserr_exist;
  1225. if (isdotent(fname, flen))
  1226. goto out;
  1227. if (!(iap->ia_valid & ATTR_MODE))
  1228. iap->ia_mode = 0;
  1229. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1230. if (err)
  1231. goto out;
  1232. dentry = fhp->fh_dentry;
  1233. dirp = dentry->d_inode;
  1234. /* Get all the sanity checks out of the way before
  1235. * we lock the parent. */
  1236. err = nfserr_notdir;
  1237. if (!dirp->i_op->lookup)
  1238. goto out;
  1239. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1240. /*
  1241. * Compose the response file handle.
  1242. */
  1243. dchild = lookup_one_len(fname, dentry, flen);
  1244. host_err = PTR_ERR(dchild);
  1245. if (IS_ERR(dchild))
  1246. goto out_nfserr;
  1247. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1248. if (err)
  1249. goto out;
  1250. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1251. /* solaris7 gets confused (bugid 4218508) if these have
  1252. * the high bit set, so just clear the high bits. If this is
  1253. * ever changed to use different attrs for storing the
  1254. * verifier, then do_open_lookup() will also need to be fixed
  1255. * accordingly.
  1256. */
  1257. v_mtime = verifier[0]&0x7fffffff;
  1258. v_atime = verifier[1]&0x7fffffff;
  1259. }
  1260. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1261. if (host_err)
  1262. goto out_nfserr;
  1263. if (dchild->d_inode) {
  1264. err = 0;
  1265. switch (createmode) {
  1266. case NFS3_CREATE_UNCHECKED:
  1267. if (! S_ISREG(dchild->d_inode->i_mode))
  1268. err = nfserr_exist;
  1269. else if (truncp) {
  1270. /* in nfsv4, we need to treat this case a little
  1271. * differently. we don't want to truncate the
  1272. * file now; this would be wrong if the OPEN
  1273. * fails for some other reason. furthermore,
  1274. * if the size is nonzero, we should ignore it
  1275. * according to spec!
  1276. */
  1277. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1278. }
  1279. else {
  1280. iap->ia_valid &= ATTR_SIZE;
  1281. goto set_attr;
  1282. }
  1283. break;
  1284. case NFS3_CREATE_EXCLUSIVE:
  1285. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1286. && dchild->d_inode->i_atime.tv_sec == v_atime
  1287. && dchild->d_inode->i_size == 0 )
  1288. break;
  1289. /* fallthru */
  1290. case NFS3_CREATE_GUARDED:
  1291. err = nfserr_exist;
  1292. }
  1293. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1294. goto out;
  1295. }
  1296. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1297. if (host_err < 0) {
  1298. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1299. goto out_nfserr;
  1300. }
  1301. if (created)
  1302. *created = 1;
  1303. if (EX_ISSYNC(fhp->fh_export)) {
  1304. err = nfserrno(nfsd_sync_dir(dentry));
  1305. /* setattr will sync the child (or not) */
  1306. }
  1307. nfsd_check_ignore_resizing(iap);
  1308. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1309. /* Cram the verifier into atime/mtime */
  1310. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1311. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1312. /* XXX someone who knows this better please fix it for nsec */
  1313. iap->ia_mtime.tv_sec = v_mtime;
  1314. iap->ia_atime.tv_sec = v_atime;
  1315. iap->ia_mtime.tv_nsec = 0;
  1316. iap->ia_atime.tv_nsec = 0;
  1317. }
  1318. set_attr:
  1319. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1320. if (err2)
  1321. err = err2;
  1322. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1323. /*
  1324. * Update the filehandle to get the new inode info.
  1325. */
  1326. if (!err)
  1327. err = fh_update(resfhp);
  1328. out:
  1329. fh_unlock(fhp);
  1330. if (dchild && !IS_ERR(dchild))
  1331. dput(dchild);
  1332. return err;
  1333. out_nfserr:
  1334. err = nfserrno(host_err);
  1335. goto out;
  1336. }
  1337. #endif /* CONFIG_NFSD_V3 */
  1338. /*
  1339. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1340. * fits into the buffer. On return, it contains the true length.
  1341. * N.B. After this call fhp needs an fh_put
  1342. */
  1343. __be32
  1344. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1345. {
  1346. struct dentry *dentry;
  1347. struct inode *inode;
  1348. mm_segment_t oldfs;
  1349. __be32 err;
  1350. int host_err;
  1351. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1352. if (err)
  1353. goto out;
  1354. dentry = fhp->fh_dentry;
  1355. inode = dentry->d_inode;
  1356. err = nfserr_inval;
  1357. if (!inode->i_op->readlink)
  1358. goto out;
  1359. touch_atime(fhp->fh_export->ex_path.mnt, dentry);
  1360. /* N.B. Why does this call need a get_fs()??
  1361. * Remove the set_fs and watch the fireworks:-) --okir
  1362. */
  1363. oldfs = get_fs(); set_fs(KERNEL_DS);
  1364. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1365. set_fs(oldfs);
  1366. if (host_err < 0)
  1367. goto out_nfserr;
  1368. *lenp = host_err;
  1369. err = 0;
  1370. out:
  1371. return err;
  1372. out_nfserr:
  1373. err = nfserrno(host_err);
  1374. goto out;
  1375. }
  1376. /*
  1377. * Create a symlink and look up its inode
  1378. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1379. */
  1380. __be32
  1381. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1382. char *fname, int flen,
  1383. char *path, int plen,
  1384. struct svc_fh *resfhp,
  1385. struct iattr *iap)
  1386. {
  1387. struct dentry *dentry, *dnew;
  1388. __be32 err, cerr;
  1389. int host_err;
  1390. err = nfserr_noent;
  1391. if (!flen || !plen)
  1392. goto out;
  1393. err = nfserr_exist;
  1394. if (isdotent(fname, flen))
  1395. goto out;
  1396. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1397. if (err)
  1398. goto out;
  1399. fh_lock(fhp);
  1400. dentry = fhp->fh_dentry;
  1401. dnew = lookup_one_len(fname, dentry, flen);
  1402. host_err = PTR_ERR(dnew);
  1403. if (IS_ERR(dnew))
  1404. goto out_nfserr;
  1405. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1406. if (host_err)
  1407. goto out_nfserr;
  1408. if (unlikely(path[plen] != 0)) {
  1409. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1410. if (path_alloced == NULL)
  1411. host_err = -ENOMEM;
  1412. else {
  1413. strncpy(path_alloced, path, plen);
  1414. path_alloced[plen] = 0;
  1415. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
  1416. kfree(path_alloced);
  1417. }
  1418. } else
  1419. host_err = vfs_symlink(dentry->d_inode, dnew, path);
  1420. if (!host_err) {
  1421. if (EX_ISSYNC(fhp->fh_export))
  1422. host_err = nfsd_sync_dir(dentry);
  1423. }
  1424. err = nfserrno(host_err);
  1425. fh_unlock(fhp);
  1426. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1427. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1428. dput(dnew);
  1429. if (err==0) err = cerr;
  1430. out:
  1431. return err;
  1432. out_nfserr:
  1433. err = nfserrno(host_err);
  1434. goto out;
  1435. }
  1436. /*
  1437. * Create a hardlink
  1438. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1439. */
  1440. __be32
  1441. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1442. char *name, int len, struct svc_fh *tfhp)
  1443. {
  1444. struct dentry *ddir, *dnew, *dold;
  1445. struct inode *dirp, *dest;
  1446. __be32 err;
  1447. int host_err;
  1448. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1449. if (err)
  1450. goto out;
  1451. err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
  1452. if (err)
  1453. goto out;
  1454. err = nfserr_perm;
  1455. if (!len)
  1456. goto out;
  1457. err = nfserr_exist;
  1458. if (isdotent(name, len))
  1459. goto out;
  1460. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1461. ddir = ffhp->fh_dentry;
  1462. dirp = ddir->d_inode;
  1463. dnew = lookup_one_len(name, ddir, len);
  1464. host_err = PTR_ERR(dnew);
  1465. if (IS_ERR(dnew))
  1466. goto out_nfserr;
  1467. dold = tfhp->fh_dentry;
  1468. dest = dold->d_inode;
  1469. host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
  1470. if (host_err) {
  1471. err = nfserrno(host_err);
  1472. goto out_dput;
  1473. }
  1474. host_err = vfs_link(dold, dirp, dnew);
  1475. if (!host_err) {
  1476. if (EX_ISSYNC(ffhp->fh_export)) {
  1477. err = nfserrno(nfsd_sync_dir(ddir));
  1478. write_inode_now(dest, 1);
  1479. }
  1480. err = 0;
  1481. } else {
  1482. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1483. err = nfserr_acces;
  1484. else
  1485. err = nfserrno(host_err);
  1486. }
  1487. mnt_drop_write(tfhp->fh_export->ex_path.mnt);
  1488. out_dput:
  1489. dput(dnew);
  1490. out_unlock:
  1491. fh_unlock(ffhp);
  1492. out:
  1493. return err;
  1494. out_nfserr:
  1495. err = nfserrno(host_err);
  1496. goto out_unlock;
  1497. }
  1498. /*
  1499. * Rename a file
  1500. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1501. */
  1502. __be32
  1503. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1504. struct svc_fh *tfhp, char *tname, int tlen)
  1505. {
  1506. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1507. struct inode *fdir, *tdir;
  1508. __be32 err;
  1509. int host_err;
  1510. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1511. if (err)
  1512. goto out;
  1513. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1514. if (err)
  1515. goto out;
  1516. fdentry = ffhp->fh_dentry;
  1517. fdir = fdentry->d_inode;
  1518. tdentry = tfhp->fh_dentry;
  1519. tdir = tdentry->d_inode;
  1520. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1521. if (ffhp->fh_export != tfhp->fh_export)
  1522. goto out;
  1523. err = nfserr_perm;
  1524. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1525. goto out;
  1526. /* cannot use fh_lock as we need deadlock protective ordering
  1527. * so do it by hand */
  1528. trap = lock_rename(tdentry, fdentry);
  1529. ffhp->fh_locked = tfhp->fh_locked = 1;
  1530. fill_pre_wcc(ffhp);
  1531. fill_pre_wcc(tfhp);
  1532. odentry = lookup_one_len(fname, fdentry, flen);
  1533. host_err = PTR_ERR(odentry);
  1534. if (IS_ERR(odentry))
  1535. goto out_nfserr;
  1536. host_err = -ENOENT;
  1537. if (!odentry->d_inode)
  1538. goto out_dput_old;
  1539. host_err = -EINVAL;
  1540. if (odentry == trap)
  1541. goto out_dput_old;
  1542. ndentry = lookup_one_len(tname, tdentry, tlen);
  1543. host_err = PTR_ERR(ndentry);
  1544. if (IS_ERR(ndentry))
  1545. goto out_dput_old;
  1546. host_err = -ENOTEMPTY;
  1547. if (ndentry == trap)
  1548. goto out_dput_new;
  1549. if (svc_msnfs(ffhp) &&
  1550. ((atomic_read(&odentry->d_count) > 1)
  1551. || (atomic_read(&ndentry->d_count) > 1))) {
  1552. host_err = -EPERM;
  1553. goto out_dput_new;
  1554. }
  1555. host_err = -EXDEV;
  1556. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1557. goto out_dput_new;
  1558. host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
  1559. if (host_err)
  1560. goto out_dput_new;
  1561. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1562. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1563. host_err = nfsd_sync_dir(tdentry);
  1564. if (!host_err)
  1565. host_err = nfsd_sync_dir(fdentry);
  1566. }
  1567. mnt_drop_write(ffhp->fh_export->ex_path.mnt);
  1568. out_dput_new:
  1569. dput(ndentry);
  1570. out_dput_old:
  1571. dput(odentry);
  1572. out_nfserr:
  1573. err = nfserrno(host_err);
  1574. /* we cannot reply on fh_unlock on the two filehandles,
  1575. * as that would do the wrong thing if the two directories
  1576. * were the same, so again we do it by hand
  1577. */
  1578. fill_post_wcc(ffhp);
  1579. fill_post_wcc(tfhp);
  1580. unlock_rename(tdentry, fdentry);
  1581. ffhp->fh_locked = tfhp->fh_locked = 0;
  1582. out:
  1583. return err;
  1584. }
  1585. /*
  1586. * Unlink a file or directory
  1587. * N.B. After this call fhp needs an fh_put
  1588. */
  1589. __be32
  1590. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1591. char *fname, int flen)
  1592. {
  1593. struct dentry *dentry, *rdentry;
  1594. struct inode *dirp;
  1595. __be32 err;
  1596. int host_err;
  1597. err = nfserr_acces;
  1598. if (!flen || isdotent(fname, flen))
  1599. goto out;
  1600. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1601. if (err)
  1602. goto out;
  1603. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1604. dentry = fhp->fh_dentry;
  1605. dirp = dentry->d_inode;
  1606. rdentry = lookup_one_len(fname, dentry, flen);
  1607. host_err = PTR_ERR(rdentry);
  1608. if (IS_ERR(rdentry))
  1609. goto out_nfserr;
  1610. if (!rdentry->d_inode) {
  1611. dput(rdentry);
  1612. err = nfserr_noent;
  1613. goto out;
  1614. }
  1615. if (!type)
  1616. type = rdentry->d_inode->i_mode & S_IFMT;
  1617. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1618. if (host_err)
  1619. goto out_nfserr;
  1620. if (type != S_IFDIR) { /* It's UNLINK */
  1621. #ifdef MSNFS
  1622. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1623. (atomic_read(&rdentry->d_count) > 1)) {
  1624. host_err = -EPERM;
  1625. } else
  1626. #endif
  1627. host_err = vfs_unlink(dirp, rdentry);
  1628. } else { /* It's RMDIR */
  1629. host_err = vfs_rmdir(dirp, rdentry);
  1630. }
  1631. dput(rdentry);
  1632. if (host_err)
  1633. goto out_drop;
  1634. if (EX_ISSYNC(fhp->fh_export))
  1635. host_err = nfsd_sync_dir(dentry);
  1636. out_drop:
  1637. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1638. out_nfserr:
  1639. err = nfserrno(host_err);
  1640. out:
  1641. return err;
  1642. }
  1643. /*
  1644. * We do this buffering because we must not call back into the file
  1645. * system's ->lookup() method from the filldir callback. That may well
  1646. * deadlock a number of file systems.
  1647. *
  1648. * This is based heavily on the implementation of same in XFS.
  1649. */
  1650. struct buffered_dirent {
  1651. u64 ino;
  1652. loff_t offset;
  1653. int namlen;
  1654. unsigned int d_type;
  1655. char name[];
  1656. };
  1657. struct readdir_data {
  1658. char *dirent;
  1659. size_t used;
  1660. int full;
  1661. };
  1662. static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
  1663. loff_t offset, u64 ino, unsigned int d_type)
  1664. {
  1665. struct readdir_data *buf = __buf;
  1666. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1667. unsigned int reclen;
  1668. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1669. if (buf->used + reclen > PAGE_SIZE) {
  1670. buf->full = 1;
  1671. return -EINVAL;
  1672. }
  1673. de->namlen = namlen;
  1674. de->offset = offset;
  1675. de->ino = ino;
  1676. de->d_type = d_type;
  1677. memcpy(de->name, name, namlen);
  1678. buf->used += reclen;
  1679. return 0;
  1680. }
  1681. static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
  1682. struct readdir_cd *cdp, loff_t *offsetp)
  1683. {
  1684. struct readdir_data buf;
  1685. struct buffered_dirent *de;
  1686. int host_err;
  1687. int size;
  1688. loff_t offset;
  1689. buf.dirent = (void *)__get_free_page(GFP_KERNEL);
  1690. if (!buf.dirent)
  1691. return nfserrno(-ENOMEM);
  1692. offset = *offsetp;
  1693. while (1) {
  1694. struct inode *dir_inode = file->f_path.dentry->d_inode;
  1695. unsigned int reclen;
  1696. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1697. buf.used = 0;
  1698. buf.full = 0;
  1699. host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
  1700. if (buf.full)
  1701. host_err = 0;
  1702. if (host_err < 0)
  1703. break;
  1704. size = buf.used;
  1705. if (!size)
  1706. break;
  1707. /*
  1708. * Various filldir functions may end up calling back into
  1709. * lookup_one_len() and the file system's ->lookup() method.
  1710. * These expect i_mutex to be held, as it would within readdir.
  1711. */
  1712. host_err = mutex_lock_killable(&dir_inode->i_mutex);
  1713. if (host_err)
  1714. break;
  1715. de = (struct buffered_dirent *)buf.dirent;
  1716. while (size > 0) {
  1717. offset = de->offset;
  1718. if (func(cdp, de->name, de->namlen, de->offset,
  1719. de->ino, de->d_type))
  1720. break;
  1721. if (cdp->err != nfs_ok)
  1722. break;
  1723. reclen = ALIGN(sizeof(*de) + de->namlen,
  1724. sizeof(u64));
  1725. size -= reclen;
  1726. de = (struct buffered_dirent *)((char *)de + reclen);
  1727. }
  1728. mutex_unlock(&dir_inode->i_mutex);
  1729. if (size > 0) /* We bailed out early */
  1730. break;
  1731. offset = vfs_llseek(file, 0, SEEK_CUR);
  1732. }
  1733. free_page((unsigned long)(buf.dirent));
  1734. if (host_err)
  1735. return nfserrno(host_err);
  1736. *offsetp = offset;
  1737. return cdp->err;
  1738. }
  1739. /*
  1740. * Read entries from a directory.
  1741. * The NFSv3/4 verifier we ignore for now.
  1742. */
  1743. __be32
  1744. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1745. struct readdir_cd *cdp, filldir_t func)
  1746. {
  1747. __be32 err;
  1748. struct file *file;
  1749. loff_t offset = *offsetp;
  1750. err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
  1751. if (err)
  1752. goto out;
  1753. offset = vfs_llseek(file, offset, 0);
  1754. if (offset < 0) {
  1755. err = nfserrno((int)offset);
  1756. goto out_close;
  1757. }
  1758. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1759. if (err == nfserr_eof || err == nfserr_toosmall)
  1760. err = nfs_ok; /* can still be found in ->err */
  1761. out_close:
  1762. nfsd_close(file);
  1763. out:
  1764. return err;
  1765. }
  1766. /*
  1767. * Get file system stats
  1768. * N.B. After this call fhp needs an fh_put
  1769. */
  1770. __be32
  1771. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1772. {
  1773. __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1774. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1775. err = nfserr_io;
  1776. return err;
  1777. }
  1778. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1779. {
  1780. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1781. }
  1782. /*
  1783. * Check for a user's access permissions to this inode.
  1784. */
  1785. __be32
  1786. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1787. struct dentry *dentry, int acc)
  1788. {
  1789. struct inode *inode = dentry->d_inode;
  1790. struct path path;
  1791. int err;
  1792. if (acc == NFSD_MAY_NOP)
  1793. return 0;
  1794. #if 0
  1795. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1796. acc,
  1797. (acc & NFSD_MAY_READ)? " read" : "",
  1798. (acc & NFSD_MAY_WRITE)? " write" : "",
  1799. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1800. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1801. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1802. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1803. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1804. inode->i_mode,
  1805. IS_IMMUTABLE(inode)? " immut" : "",
  1806. IS_APPEND(inode)? " append" : "",
  1807. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1808. dprintk(" owner %d/%d user %d/%d\n",
  1809. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1810. #endif
  1811. /* Normally we reject any write/sattr etc access on a read-only file
  1812. * system. But if it is IRIX doing check on write-access for a
  1813. * device special file, we ignore rofs.
  1814. */
  1815. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1816. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1817. if (exp_rdonly(rqstp, exp) ||
  1818. __mnt_is_readonly(exp->ex_path.mnt))
  1819. return nfserr_rofs;
  1820. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1821. return nfserr_perm;
  1822. }
  1823. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1824. return nfserr_perm;
  1825. if (acc & NFSD_MAY_LOCK) {
  1826. /* If we cannot rely on authentication in NLM requests,
  1827. * just allow locks, otherwise require read permission, or
  1828. * ownership
  1829. */
  1830. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1831. return 0;
  1832. else
  1833. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1834. }
  1835. /*
  1836. * The file owner always gets access permission for accesses that
  1837. * would normally be checked at open time. This is to make
  1838. * file access work even when the client has done a fchmod(fd, 0).
  1839. *
  1840. * However, `cp foo bar' should fail nevertheless when bar is
  1841. * readonly. A sensible way to do this might be to reject all
  1842. * attempts to truncate a read-only file, because a creat() call
  1843. * always implies file truncation.
  1844. * ... but this isn't really fair. A process may reasonably call
  1845. * ftruncate on an open file descriptor on a file with perm 000.
  1846. * We must trust the client to do permission checking - using "ACCESS"
  1847. * with NFSv3.
  1848. */
  1849. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1850. inode->i_uid == current_fsuid())
  1851. return 0;
  1852. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1853. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1854. /* Allow read access to binaries even when mode 111 */
  1855. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1856. acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
  1857. err = inode_permission(inode, MAY_EXEC);
  1858. if (err)
  1859. goto nfsd_out;
  1860. /* Do integrity (permission) checking now, but defer incrementing
  1861. * IMA counts to the actual file open.
  1862. */
  1863. path.mnt = exp->ex_path.mnt;
  1864. path.dentry = dentry;
  1865. err = ima_path_check(&path, acc & (MAY_READ | MAY_WRITE | MAY_EXEC));
  1866. nfsd_out:
  1867. return err? nfserrno(err) : 0;
  1868. }
  1869. void
  1870. nfsd_racache_shutdown(void)
  1871. {
  1872. struct raparms *raparm, *last_raparm;
  1873. unsigned int i;
  1874. dprintk("nfsd: freeing readahead buffers.\n");
  1875. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1876. raparm = raparm_hash[i].pb_head;
  1877. while(raparm) {
  1878. last_raparm = raparm;
  1879. raparm = raparm->p_next;
  1880. kfree(last_raparm);
  1881. }
  1882. raparm_hash[i].pb_head = NULL;
  1883. }
  1884. }
  1885. /*
  1886. * Initialize readahead param cache
  1887. */
  1888. int
  1889. nfsd_racache_init(int cache_size)
  1890. {
  1891. int i;
  1892. int j = 0;
  1893. int nperbucket;
  1894. struct raparms **raparm = NULL;
  1895. if (raparm_hash[0].pb_head)
  1896. return 0;
  1897. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1898. if (nperbucket < 2)
  1899. nperbucket = 2;
  1900. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1901. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1902. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1903. spin_lock_init(&raparm_hash[i].pb_lock);
  1904. raparm = &raparm_hash[i].pb_head;
  1905. for (j = 0; j < nperbucket; j++) {
  1906. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1907. if (!*raparm)
  1908. goto out_nomem;
  1909. raparm = &(*raparm)->p_next;
  1910. }
  1911. *raparm = NULL;
  1912. }
  1913. nfsdstats.ra_size = cache_size;
  1914. return 0;
  1915. out_nomem:
  1916. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1917. nfsd_racache_shutdown();
  1918. return -ENOMEM;
  1919. }
  1920. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1921. struct posix_acl *
  1922. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1923. {
  1924. struct inode *inode = fhp->fh_dentry->d_inode;
  1925. char *name;
  1926. void *value = NULL;
  1927. ssize_t size;
  1928. struct posix_acl *acl;
  1929. if (!IS_POSIXACL(inode))
  1930. return ERR_PTR(-EOPNOTSUPP);
  1931. switch (type) {
  1932. case ACL_TYPE_ACCESS:
  1933. name = POSIX_ACL_XATTR_ACCESS;
  1934. break;
  1935. case ACL_TYPE_DEFAULT:
  1936. name = POSIX_ACL_XATTR_DEFAULT;
  1937. break;
  1938. default:
  1939. return ERR_PTR(-EOPNOTSUPP);
  1940. }
  1941. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1942. if (size < 0)
  1943. return ERR_PTR(size);
  1944. acl = posix_acl_from_xattr(value, size);
  1945. kfree(value);
  1946. return acl;
  1947. }
  1948. int
  1949. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1950. {
  1951. struct inode *inode = fhp->fh_dentry->d_inode;
  1952. char *name;
  1953. void *value = NULL;
  1954. size_t size;
  1955. int error;
  1956. if (!IS_POSIXACL(inode) ||
  1957. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1958. return -EOPNOTSUPP;
  1959. switch(type) {
  1960. case ACL_TYPE_ACCESS:
  1961. name = POSIX_ACL_XATTR_ACCESS;
  1962. break;
  1963. case ACL_TYPE_DEFAULT:
  1964. name = POSIX_ACL_XATTR_DEFAULT;
  1965. break;
  1966. default:
  1967. return -EOPNOTSUPP;
  1968. }
  1969. if (acl && acl->a_count) {
  1970. size = posix_acl_xattr_size(acl->a_count);
  1971. value = kmalloc(size, GFP_KERNEL);
  1972. if (!value)
  1973. return -ENOMEM;
  1974. error = posix_acl_to_xattr(acl, value, size);
  1975. if (error < 0)
  1976. goto getout;
  1977. size = error;
  1978. } else
  1979. size = 0;
  1980. error = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1981. if (error)
  1982. goto getout;
  1983. if (size)
  1984. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1985. else {
  1986. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1987. error = 0;
  1988. else {
  1989. error = vfs_removexattr(fhp->fh_dentry, name);
  1990. if (error == -ENODATA)
  1991. error = 0;
  1992. }
  1993. }
  1994. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1995. getout:
  1996. kfree(value);
  1997. return error;
  1998. }
  1999. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */