vfs.c 45 KB

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