vfs.c 45 KB

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