dir.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/sched.h>
  36. #include "nfs4_fs.h"
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. /* #define NFS_DEBUG_VERBOSE 1 */
  40. static int nfs_opendir(struct inode *, struct file *);
  41. static int nfs_readdir(struct file *, void *, filldir_t);
  42. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  43. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  44. static int nfs_mkdir(struct inode *, struct dentry *, int);
  45. static int nfs_rmdir(struct inode *, struct dentry *);
  46. static int nfs_unlink(struct inode *, struct dentry *);
  47. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  48. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  49. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  50. static int nfs_rename(struct inode *, struct dentry *,
  51. struct inode *, struct dentry *);
  52. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  53. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  54. const struct file_operations nfs_dir_operations = {
  55. .llseek = nfs_llseek_dir,
  56. .read = generic_read_dir,
  57. .readdir = nfs_readdir,
  58. .open = nfs_opendir,
  59. .release = nfs_release,
  60. .fsync = nfs_fsync_dir,
  61. };
  62. const struct inode_operations nfs_dir_inode_operations = {
  63. .create = nfs_create,
  64. .lookup = nfs_lookup,
  65. .link = nfs_link,
  66. .unlink = nfs_unlink,
  67. .symlink = nfs_symlink,
  68. .mkdir = nfs_mkdir,
  69. .rmdir = nfs_rmdir,
  70. .mknod = nfs_mknod,
  71. .rename = nfs_rename,
  72. .permission = nfs_permission,
  73. .getattr = nfs_getattr,
  74. .setattr = nfs_setattr,
  75. };
  76. #ifdef CONFIG_NFS_V3
  77. const struct inode_operations nfs3_dir_inode_operations = {
  78. .create = nfs_create,
  79. .lookup = nfs_lookup,
  80. .link = nfs_link,
  81. .unlink = nfs_unlink,
  82. .symlink = nfs_symlink,
  83. .mkdir = nfs_mkdir,
  84. .rmdir = nfs_rmdir,
  85. .mknod = nfs_mknod,
  86. .rename = nfs_rename,
  87. .permission = nfs_permission,
  88. .getattr = nfs_getattr,
  89. .setattr = nfs_setattr,
  90. .listxattr = nfs3_listxattr,
  91. .getxattr = nfs3_getxattr,
  92. .setxattr = nfs3_setxattr,
  93. .removexattr = nfs3_removexattr,
  94. };
  95. #endif /* CONFIG_NFS_V3 */
  96. #ifdef CONFIG_NFS_V4
  97. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  98. const struct inode_operations nfs4_dir_inode_operations = {
  99. .create = nfs_create,
  100. .lookup = nfs_atomic_lookup,
  101. .link = nfs_link,
  102. .unlink = nfs_unlink,
  103. .symlink = nfs_symlink,
  104. .mkdir = nfs_mkdir,
  105. .rmdir = nfs_rmdir,
  106. .mknod = nfs_mknod,
  107. .rename = nfs_rename,
  108. .permission = nfs_permission,
  109. .getattr = nfs_getattr,
  110. .setattr = nfs_setattr,
  111. .getxattr = nfs4_getxattr,
  112. .setxattr = nfs4_setxattr,
  113. .listxattr = nfs4_listxattr,
  114. };
  115. #endif /* CONFIG_NFS_V4 */
  116. /*
  117. * Open file
  118. */
  119. static int
  120. nfs_opendir(struct inode *inode, struct file *filp)
  121. {
  122. int res;
  123. dfprintk(VFS, "NFS: opendir(%s/%ld)\n",
  124. inode->i_sb->s_id, inode->i_ino);
  125. lock_kernel();
  126. /* Call generic open code in order to cache credentials */
  127. res = nfs_open(inode, filp);
  128. unlock_kernel();
  129. return res;
  130. }
  131. typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
  132. typedef struct {
  133. struct file *file;
  134. struct page *page;
  135. unsigned long page_index;
  136. __be32 *ptr;
  137. u64 *dir_cookie;
  138. loff_t current_index;
  139. struct nfs_entry *entry;
  140. decode_dirent_t decode;
  141. int plus;
  142. int error;
  143. unsigned long timestamp;
  144. int timestamp_valid;
  145. } nfs_readdir_descriptor_t;
  146. /* Now we cache directories properly, by stuffing the dirent
  147. * data directly in the page cache.
  148. *
  149. * Inode invalidation due to refresh etc. takes care of
  150. * _everything_, no sloppy entry flushing logic, no extraneous
  151. * copying, network direct to page cache, the way it was meant
  152. * to be.
  153. *
  154. * NOTE: Dirent information verification is done always by the
  155. * page-in of the RPC reply, nowhere else, this simplies
  156. * things substantially.
  157. */
  158. static
  159. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  160. {
  161. struct file *file = desc->file;
  162. struct inode *inode = file->f_path.dentry->d_inode;
  163. struct rpc_cred *cred = nfs_file_cred(file);
  164. unsigned long timestamp;
  165. int error;
  166. dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
  167. __FUNCTION__, (long long)desc->entry->cookie,
  168. page->index);
  169. again:
  170. timestamp = jiffies;
  171. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
  172. NFS_SERVER(inode)->dtsize, desc->plus);
  173. if (error < 0) {
  174. /* We requested READDIRPLUS, but the server doesn't grok it */
  175. if (error == -ENOTSUPP && desc->plus) {
  176. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  177. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  178. desc->plus = 0;
  179. goto again;
  180. }
  181. goto error;
  182. }
  183. desc->timestamp = timestamp;
  184. desc->timestamp_valid = 1;
  185. SetPageUptodate(page);
  186. /* Ensure consistent page alignment of the data.
  187. * Note: assumes we have exclusive access to this mapping either
  188. * through inode->i_mutex or some other mechanism.
  189. */
  190. if (page->index == 0 && invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1) < 0) {
  191. /* Should never happen */
  192. nfs_zap_mapping(inode, inode->i_mapping);
  193. }
  194. unlock_page(page);
  195. return 0;
  196. error:
  197. SetPageError(page);
  198. unlock_page(page);
  199. nfs_zap_caches(inode);
  200. desc->error = error;
  201. return -EIO;
  202. }
  203. static inline
  204. int dir_decode(nfs_readdir_descriptor_t *desc)
  205. {
  206. __be32 *p = desc->ptr;
  207. p = desc->decode(p, desc->entry, desc->plus);
  208. if (IS_ERR(p))
  209. return PTR_ERR(p);
  210. desc->ptr = p;
  211. if (desc->timestamp_valid)
  212. desc->entry->fattr->time_start = desc->timestamp;
  213. else
  214. desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
  215. return 0;
  216. }
  217. static inline
  218. void dir_page_release(nfs_readdir_descriptor_t *desc)
  219. {
  220. kunmap(desc->page);
  221. page_cache_release(desc->page);
  222. desc->page = NULL;
  223. desc->ptr = NULL;
  224. }
  225. /*
  226. * Given a pointer to a buffer that has already been filled by a call
  227. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  228. *
  229. * If the end of the buffer has been reached, return -EAGAIN, if not,
  230. * return the offset within the buffer of the next entry to be
  231. * read.
  232. */
  233. static inline
  234. int find_dirent(nfs_readdir_descriptor_t *desc)
  235. {
  236. struct nfs_entry *entry = desc->entry;
  237. int loop_count = 0,
  238. status;
  239. while((status = dir_decode(desc)) == 0) {
  240. dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
  241. __FUNCTION__, (unsigned long long)entry->cookie);
  242. if (entry->prev_cookie == *desc->dir_cookie)
  243. break;
  244. if (loop_count++ > 200) {
  245. loop_count = 0;
  246. schedule();
  247. }
  248. }
  249. return status;
  250. }
  251. /*
  252. * Given a pointer to a buffer that has already been filled by a call
  253. * to readdir, find the entry at offset 'desc->file->f_pos'.
  254. *
  255. * If the end of the buffer has been reached, return -EAGAIN, if not,
  256. * return the offset within the buffer of the next entry to be
  257. * read.
  258. */
  259. static inline
  260. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  261. {
  262. struct nfs_entry *entry = desc->entry;
  263. int loop_count = 0,
  264. status;
  265. for(;;) {
  266. status = dir_decode(desc);
  267. if (status)
  268. break;
  269. dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
  270. (unsigned long long)entry->cookie, desc->current_index);
  271. if (desc->file->f_pos == desc->current_index) {
  272. *desc->dir_cookie = entry->cookie;
  273. break;
  274. }
  275. desc->current_index++;
  276. if (loop_count++ > 200) {
  277. loop_count = 0;
  278. schedule();
  279. }
  280. }
  281. return status;
  282. }
  283. /*
  284. * Find the given page, and call find_dirent() or find_dirent_index in
  285. * order to try to return the next entry.
  286. */
  287. static inline
  288. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  289. {
  290. struct inode *inode = desc->file->f_path.dentry->d_inode;
  291. struct page *page;
  292. int status;
  293. dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
  294. __FUNCTION__, desc->page_index,
  295. (long long) *desc->dir_cookie);
  296. /* If we find the page in the page_cache, we cannot be sure
  297. * how fresh the data is, so we will ignore readdir_plus attributes.
  298. */
  299. desc->timestamp_valid = 0;
  300. page = read_cache_page(inode->i_mapping, desc->page_index,
  301. (filler_t *)nfs_readdir_filler, desc);
  302. if (IS_ERR(page)) {
  303. status = PTR_ERR(page);
  304. goto out;
  305. }
  306. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  307. desc->page = page;
  308. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  309. if (*desc->dir_cookie != 0)
  310. status = find_dirent(desc);
  311. else
  312. status = find_dirent_index(desc);
  313. if (status < 0)
  314. dir_page_release(desc);
  315. out:
  316. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, status);
  317. return status;
  318. }
  319. /*
  320. * Recurse through the page cache pages, and return a
  321. * filled nfs_entry structure of the next directory entry if possible.
  322. *
  323. * The target for the search is '*desc->dir_cookie' if non-0,
  324. * 'desc->file->f_pos' otherwise
  325. */
  326. static inline
  327. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  328. {
  329. int loop_count = 0;
  330. int res;
  331. /* Always search-by-index from the beginning of the cache */
  332. if (*desc->dir_cookie == 0) {
  333. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
  334. (long long)desc->file->f_pos);
  335. desc->page_index = 0;
  336. desc->entry->cookie = desc->entry->prev_cookie = 0;
  337. desc->entry->eof = 0;
  338. desc->current_index = 0;
  339. } else
  340. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
  341. (unsigned long long)*desc->dir_cookie);
  342. for (;;) {
  343. res = find_dirent_page(desc);
  344. if (res != -EAGAIN)
  345. break;
  346. /* Align to beginning of next page */
  347. desc->page_index ++;
  348. if (loop_count++ > 200) {
  349. loop_count = 0;
  350. schedule();
  351. }
  352. }
  353. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __FUNCTION__, res);
  354. return res;
  355. }
  356. static inline unsigned int dt_type(struct inode *inode)
  357. {
  358. return (inode->i_mode >> 12) & 15;
  359. }
  360. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  361. /*
  362. * Once we've found the start of the dirent within a page: fill 'er up...
  363. */
  364. static
  365. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  366. filldir_t filldir)
  367. {
  368. struct file *file = desc->file;
  369. struct nfs_entry *entry = desc->entry;
  370. struct dentry *dentry = NULL;
  371. u64 fileid;
  372. int loop_count = 0,
  373. res;
  374. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
  375. (unsigned long long)entry->cookie);
  376. for(;;) {
  377. unsigned d_type = DT_UNKNOWN;
  378. /* Note: entry->prev_cookie contains the cookie for
  379. * retrieving the current dirent on the server */
  380. fileid = entry->ino;
  381. /* Get a dentry if we have one */
  382. if (dentry != NULL)
  383. dput(dentry);
  384. dentry = nfs_readdir_lookup(desc);
  385. /* Use readdirplus info */
  386. if (dentry != NULL && dentry->d_inode != NULL) {
  387. d_type = dt_type(dentry->d_inode);
  388. fileid = NFS_FILEID(dentry->d_inode);
  389. }
  390. res = filldir(dirent, entry->name, entry->len,
  391. file->f_pos, fileid, d_type);
  392. if (res < 0)
  393. break;
  394. file->f_pos++;
  395. *desc->dir_cookie = entry->cookie;
  396. if (dir_decode(desc) != 0) {
  397. desc->page_index ++;
  398. break;
  399. }
  400. if (loop_count++ > 200) {
  401. loop_count = 0;
  402. schedule();
  403. }
  404. }
  405. dir_page_release(desc);
  406. if (dentry != NULL)
  407. dput(dentry);
  408. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  409. (unsigned long long)*desc->dir_cookie, res);
  410. return res;
  411. }
  412. /*
  413. * If we cannot find a cookie in our cache, we suspect that this is
  414. * because it points to a deleted file, so we ask the server to return
  415. * whatever it thinks is the next entry. We then feed this to filldir.
  416. * If all goes well, we should then be able to find our way round the
  417. * cache on the next call to readdir_search_pagecache();
  418. *
  419. * NOTE: we cannot add the anonymous page to the pagecache because
  420. * the data it contains might not be page aligned. Besides,
  421. * we should already have a complete representation of the
  422. * directory in the page cache by the time we get here.
  423. */
  424. static inline
  425. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  426. filldir_t filldir)
  427. {
  428. struct file *file = desc->file;
  429. struct inode *inode = file->f_path.dentry->d_inode;
  430. struct rpc_cred *cred = nfs_file_cred(file);
  431. struct page *page = NULL;
  432. int status;
  433. unsigned long timestamp;
  434. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  435. (unsigned long long)*desc->dir_cookie);
  436. page = alloc_page(GFP_HIGHUSER);
  437. if (!page) {
  438. status = -ENOMEM;
  439. goto out;
  440. }
  441. timestamp = jiffies;
  442. desc->error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, *desc->dir_cookie,
  443. page,
  444. NFS_SERVER(inode)->dtsize,
  445. desc->plus);
  446. desc->page = page;
  447. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  448. if (desc->error >= 0) {
  449. desc->timestamp = timestamp;
  450. desc->timestamp_valid = 1;
  451. if ((status = dir_decode(desc)) == 0)
  452. desc->entry->prev_cookie = *desc->dir_cookie;
  453. } else
  454. status = -EIO;
  455. if (status < 0)
  456. goto out_release;
  457. status = nfs_do_filldir(desc, dirent, filldir);
  458. /* Reset read descriptor so it searches the page cache from
  459. * the start upon the next call to readdir_search_pagecache() */
  460. desc->page_index = 0;
  461. desc->entry->cookie = desc->entry->prev_cookie = 0;
  462. desc->entry->eof = 0;
  463. out:
  464. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  465. __FUNCTION__, status);
  466. return status;
  467. out_release:
  468. dir_page_release(desc);
  469. goto out;
  470. }
  471. /* The file offset position represents the dirent entry number. A
  472. last cookie cache takes care of the common case of reading the
  473. whole directory.
  474. */
  475. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  476. {
  477. struct dentry *dentry = filp->f_path.dentry;
  478. struct inode *inode = dentry->d_inode;
  479. nfs_readdir_descriptor_t my_desc,
  480. *desc = &my_desc;
  481. struct nfs_entry my_entry;
  482. struct nfs_fh fh;
  483. struct nfs_fattr fattr;
  484. long res;
  485. dfprintk(VFS, "NFS: readdir(%s/%s) starting at cookie %Lu\n",
  486. dentry->d_parent->d_name.name, dentry->d_name.name,
  487. (long long)filp->f_pos);
  488. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  489. lock_kernel();
  490. res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
  491. if (res < 0) {
  492. unlock_kernel();
  493. return res;
  494. }
  495. /*
  496. * filp->f_pos points to the dirent entry number.
  497. * *desc->dir_cookie has the cookie for the next entry. We have
  498. * to either find the entry with the appropriate number or
  499. * revalidate the cookie.
  500. */
  501. memset(desc, 0, sizeof(*desc));
  502. desc->file = filp;
  503. desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
  504. desc->decode = NFS_PROTO(inode)->decode_dirent;
  505. desc->plus = NFS_USE_READDIRPLUS(inode);
  506. my_entry.cookie = my_entry.prev_cookie = 0;
  507. my_entry.eof = 0;
  508. my_entry.fh = &fh;
  509. my_entry.fattr = &fattr;
  510. nfs_fattr_init(&fattr);
  511. desc->entry = &my_entry;
  512. while(!desc->entry->eof) {
  513. res = readdir_search_pagecache(desc);
  514. if (res == -EBADCOOKIE) {
  515. /* This means either end of directory */
  516. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  517. /* Or that the server has 'lost' a cookie */
  518. res = uncached_readdir(desc, dirent, filldir);
  519. if (res >= 0)
  520. continue;
  521. }
  522. res = 0;
  523. break;
  524. }
  525. if (res == -ETOOSMALL && desc->plus) {
  526. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_FLAGS(inode));
  527. nfs_zap_caches(inode);
  528. desc->plus = 0;
  529. desc->entry->eof = 0;
  530. continue;
  531. }
  532. if (res < 0)
  533. break;
  534. res = nfs_do_filldir(desc, dirent, filldir);
  535. if (res < 0) {
  536. res = 0;
  537. break;
  538. }
  539. }
  540. unlock_kernel();
  541. if (res > 0)
  542. res = 0;
  543. dfprintk(VFS, "NFS: readdir(%s/%s) returns %ld\n",
  544. dentry->d_parent->d_name.name, dentry->d_name.name,
  545. res);
  546. return res;
  547. }
  548. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  549. {
  550. mutex_lock(&filp->f_path.dentry->d_inode->i_mutex);
  551. switch (origin) {
  552. case 1:
  553. offset += filp->f_pos;
  554. case 0:
  555. if (offset >= 0)
  556. break;
  557. default:
  558. offset = -EINVAL;
  559. goto out;
  560. }
  561. if (offset != filp->f_pos) {
  562. filp->f_pos = offset;
  563. nfs_file_open_context(filp)->dir_cookie = 0;
  564. }
  565. out:
  566. mutex_unlock(&filp->f_path.dentry->d_inode->i_mutex);
  567. return offset;
  568. }
  569. /*
  570. * All directory operations under NFS are synchronous, so fsync()
  571. * is a dummy operation.
  572. */
  573. static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  574. {
  575. dfprintk(VFS, "NFS: fsync_dir(%s/%s) datasync %d\n",
  576. dentry->d_parent->d_name.name, dentry->d_name.name,
  577. datasync);
  578. return 0;
  579. }
  580. /*
  581. * A check for whether or not the parent directory has changed.
  582. * In the case it has, we assume that the dentries are untrustworthy
  583. * and may need to be looked up again.
  584. */
  585. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  586. {
  587. if (IS_ROOT(dentry))
  588. return 1;
  589. if (nfs_verify_change_attribute(dir, dentry->d_time))
  590. return 1;
  591. return 0;
  592. }
  593. static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
  594. {
  595. dentry->d_time = verf;
  596. }
  597. /*
  598. * Return the intent data that applies to this particular path component
  599. *
  600. * Note that the current set of intents only apply to the very last
  601. * component of the path.
  602. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  603. */
  604. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  605. {
  606. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  607. return 0;
  608. return nd->flags & mask;
  609. }
  610. /*
  611. * Inode and filehandle revalidation for lookups.
  612. *
  613. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  614. * or if the intent information indicates that we're about to open this
  615. * particular file and the "nocto" mount flag is not set.
  616. *
  617. */
  618. static inline
  619. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  620. {
  621. struct nfs_server *server = NFS_SERVER(inode);
  622. if (nd != NULL) {
  623. /* VFS wants an on-the-wire revalidation */
  624. if (nd->flags & LOOKUP_REVAL)
  625. goto out_force;
  626. /* This is an open(2) */
  627. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  628. !(server->flags & NFS_MOUNT_NOCTO) &&
  629. (S_ISREG(inode->i_mode) ||
  630. S_ISDIR(inode->i_mode)))
  631. goto out_force;
  632. }
  633. return nfs_revalidate_inode(server, inode);
  634. out_force:
  635. return __nfs_revalidate_inode(server, inode);
  636. }
  637. /*
  638. * We judge how long we want to trust negative
  639. * dentries by looking at the parent inode mtime.
  640. *
  641. * If parent mtime has changed, we revalidate, else we wait for a
  642. * period corresponding to the parent's attribute cache timeout value.
  643. */
  644. static inline
  645. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  646. struct nameidata *nd)
  647. {
  648. /* Don't revalidate a negative dentry if we're creating a new file */
  649. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  650. return 0;
  651. return !nfs_check_verifier(dir, dentry);
  652. }
  653. /*
  654. * This is called every time the dcache has a lookup hit,
  655. * and we should check whether we can really trust that
  656. * lookup.
  657. *
  658. * NOTE! The hit can be a negative hit too, don't assume
  659. * we have an inode!
  660. *
  661. * If the parent directory is seen to have changed, we throw out the
  662. * cached dentry and do a new lookup.
  663. */
  664. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  665. {
  666. struct inode *dir;
  667. struct inode *inode;
  668. struct dentry *parent;
  669. int error;
  670. struct nfs_fh fhandle;
  671. struct nfs_fattr fattr;
  672. unsigned long verifier;
  673. parent = dget_parent(dentry);
  674. lock_kernel();
  675. dir = parent->d_inode;
  676. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  677. inode = dentry->d_inode;
  678. /* Revalidate parent directory attribute cache */
  679. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  680. goto out_zap_parent;
  681. if (!inode) {
  682. if (nfs_neg_need_reval(dir, dentry, nd))
  683. goto out_bad;
  684. goto out_valid;
  685. }
  686. if (is_bad_inode(inode)) {
  687. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  688. __FUNCTION__, dentry->d_parent->d_name.name,
  689. dentry->d_name.name);
  690. goto out_bad;
  691. }
  692. /* Force a full look up iff the parent directory has changed */
  693. if (nfs_check_verifier(dir, dentry)) {
  694. if (nfs_lookup_verify_inode(inode, nd))
  695. goto out_zap_parent;
  696. goto out_valid;
  697. }
  698. if (NFS_STALE(inode))
  699. goto out_bad;
  700. verifier = nfs_save_change_attribute(dir);
  701. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  702. if (error)
  703. goto out_bad;
  704. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  705. goto out_bad;
  706. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  707. goto out_bad;
  708. nfs_set_verifier(dentry, verifier);
  709. out_valid:
  710. unlock_kernel();
  711. dput(parent);
  712. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  713. __FUNCTION__, dentry->d_parent->d_name.name,
  714. dentry->d_name.name);
  715. return 1;
  716. out_zap_parent:
  717. nfs_zap_caches(dir);
  718. out_bad:
  719. NFS_CACHEINV(dir);
  720. if (inode && S_ISDIR(inode->i_mode)) {
  721. /* Purge readdir caches. */
  722. nfs_zap_caches(inode);
  723. /* If we have submounts, don't unhash ! */
  724. if (have_submounts(dentry))
  725. goto out_valid;
  726. shrink_dcache_parent(dentry);
  727. }
  728. d_drop(dentry);
  729. unlock_kernel();
  730. dput(parent);
  731. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  732. __FUNCTION__, dentry->d_parent->d_name.name,
  733. dentry->d_name.name);
  734. return 0;
  735. }
  736. /*
  737. * This is called from dput() when d_count is going to 0.
  738. */
  739. static int nfs_dentry_delete(struct dentry *dentry)
  740. {
  741. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  742. dentry->d_parent->d_name.name, dentry->d_name.name,
  743. dentry->d_flags);
  744. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  745. /* Unhash it, so that ->d_iput() would be called */
  746. return 1;
  747. }
  748. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  749. /* Unhash it, so that ancestors of killed async unlink
  750. * files will be cleaned up during umount */
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. /*
  756. * Called when the dentry loses inode.
  757. * We use it to clean up silly-renamed files.
  758. */
  759. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  760. {
  761. nfs_inode_return_delegation(inode);
  762. if (S_ISDIR(inode->i_mode))
  763. /* drop any readdir cache as it could easily be old */
  764. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  765. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  766. lock_kernel();
  767. drop_nlink(inode);
  768. nfs_complete_unlink(dentry, inode);
  769. unlock_kernel();
  770. }
  771. iput(inode);
  772. }
  773. struct dentry_operations nfs_dentry_operations = {
  774. .d_revalidate = nfs_lookup_revalidate,
  775. .d_delete = nfs_dentry_delete,
  776. .d_iput = nfs_dentry_iput,
  777. };
  778. /*
  779. * Use intent information to check whether or not we're going to do
  780. * an O_EXCL create using this path component.
  781. */
  782. static inline
  783. int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  784. {
  785. if (NFS_PROTO(dir)->version == 2)
  786. return 0;
  787. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
  788. return 0;
  789. return (nd->intent.open.flags & O_EXCL) != 0;
  790. }
  791. static inline int nfs_reval_fsid(struct inode *dir, const struct nfs_fattr *fattr)
  792. {
  793. struct nfs_server *server = NFS_SERVER(dir);
  794. if (!nfs_fsid_equal(&server->fsid, &fattr->fsid))
  795. /* Revalidate fsid using the parent directory */
  796. return __nfs_revalidate_inode(server, dir);
  797. return 0;
  798. }
  799. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  800. {
  801. struct dentry *res;
  802. struct inode *inode = NULL;
  803. int error;
  804. struct nfs_fh fhandle;
  805. struct nfs_fattr fattr;
  806. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  807. dentry->d_parent->d_name.name, dentry->d_name.name);
  808. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  809. res = ERR_PTR(-ENAMETOOLONG);
  810. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  811. goto out;
  812. res = ERR_PTR(-ENOMEM);
  813. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  814. lock_kernel();
  815. /*
  816. * If we're doing an exclusive create, optimize away the lookup
  817. * but don't hash the dentry.
  818. */
  819. if (nfs_is_exclusive_create(dir, nd)) {
  820. d_instantiate(dentry, NULL);
  821. res = NULL;
  822. goto out_unlock;
  823. }
  824. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  825. if (error == -ENOENT)
  826. goto no_entry;
  827. if (error < 0) {
  828. res = ERR_PTR(error);
  829. goto out_unlock;
  830. }
  831. error = nfs_reval_fsid(dir, &fattr);
  832. if (error < 0) {
  833. res = ERR_PTR(error);
  834. goto out_unlock;
  835. }
  836. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  837. res = (struct dentry *)inode;
  838. if (IS_ERR(res))
  839. goto out_unlock;
  840. no_entry:
  841. res = d_materialise_unique(dentry, inode);
  842. if (res != NULL) {
  843. struct dentry *parent;
  844. if (IS_ERR(res))
  845. goto out_unlock;
  846. /* Was a directory renamed! */
  847. parent = dget_parent(res);
  848. if (!IS_ROOT(parent))
  849. nfs_mark_for_revalidate(parent->d_inode);
  850. dput(parent);
  851. dentry = res;
  852. }
  853. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  854. out_unlock:
  855. unlock_kernel();
  856. out:
  857. return res;
  858. }
  859. #ifdef CONFIG_NFS_V4
  860. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  861. struct dentry_operations nfs4_dentry_operations = {
  862. .d_revalidate = nfs_open_revalidate,
  863. .d_delete = nfs_dentry_delete,
  864. .d_iput = nfs_dentry_iput,
  865. };
  866. /*
  867. * Use intent information to determine whether we need to substitute
  868. * the NFSv4-style stateful OPEN for the LOOKUP call
  869. */
  870. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  871. {
  872. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  873. return 0;
  874. /* NFS does not (yet) have a stateful open for directories */
  875. if (nd->flags & LOOKUP_DIRECTORY)
  876. return 0;
  877. /* Are we trying to write to a read only partition? */
  878. if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  879. return 0;
  880. return 1;
  881. }
  882. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  883. {
  884. struct dentry *res = NULL;
  885. int error;
  886. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  887. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  888. /* Check that we are indeed trying to open this file */
  889. if (!is_atomic_open(dir, nd))
  890. goto no_open;
  891. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  892. res = ERR_PTR(-ENAMETOOLONG);
  893. goto out;
  894. }
  895. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  896. /* Let vfs_create() deal with O_EXCL */
  897. if (nd->intent.open.flags & O_EXCL) {
  898. d_add(dentry, NULL);
  899. goto out;
  900. }
  901. /* Open the file on the server */
  902. lock_kernel();
  903. /* Revalidate parent directory attribute cache */
  904. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  905. if (error < 0) {
  906. res = ERR_PTR(error);
  907. unlock_kernel();
  908. goto out;
  909. }
  910. if (nd->intent.open.flags & O_CREAT) {
  911. nfs_begin_data_update(dir);
  912. res = nfs4_atomic_open(dir, dentry, nd);
  913. nfs_end_data_update(dir);
  914. } else
  915. res = nfs4_atomic_open(dir, dentry, nd);
  916. unlock_kernel();
  917. if (IS_ERR(res)) {
  918. error = PTR_ERR(res);
  919. switch (error) {
  920. /* Make a negative dentry */
  921. case -ENOENT:
  922. res = NULL;
  923. goto out;
  924. /* This turned out not to be a regular file */
  925. case -EISDIR:
  926. case -ENOTDIR:
  927. goto no_open;
  928. case -ELOOP:
  929. if (!(nd->intent.open.flags & O_NOFOLLOW))
  930. goto no_open;
  931. /* case -EINVAL: */
  932. default:
  933. goto out;
  934. }
  935. } else if (res != NULL)
  936. dentry = res;
  937. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  938. out:
  939. return res;
  940. no_open:
  941. return nfs_lookup(dir, dentry, nd);
  942. }
  943. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  944. {
  945. struct dentry *parent = NULL;
  946. struct inode *inode = dentry->d_inode;
  947. struct inode *dir;
  948. unsigned long verifier;
  949. int openflags, ret = 0;
  950. parent = dget_parent(dentry);
  951. dir = parent->d_inode;
  952. if (!is_atomic_open(dir, nd))
  953. goto no_open;
  954. /* We can't create new files in nfs_open_revalidate(), so we
  955. * optimize away revalidation of negative dentries.
  956. */
  957. if (inode == NULL)
  958. goto out;
  959. /* NFS only supports OPEN on regular files */
  960. if (!S_ISREG(inode->i_mode))
  961. goto no_open;
  962. openflags = nd->intent.open.flags;
  963. /* We cannot do exclusive creation on a positive dentry */
  964. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  965. goto no_open;
  966. /* We can't create new files, or truncate existing ones here */
  967. openflags &= ~(O_CREAT|O_TRUNC);
  968. /*
  969. * Note: we're not holding inode->i_mutex and so may be racing with
  970. * operations that change the directory. We therefore save the
  971. * change attribute *before* we do the RPC call.
  972. */
  973. lock_kernel();
  974. verifier = nfs_save_change_attribute(dir);
  975. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  976. if (!ret)
  977. nfs_set_verifier(dentry, verifier);
  978. unlock_kernel();
  979. out:
  980. dput(parent);
  981. if (!ret)
  982. d_drop(dentry);
  983. return ret;
  984. no_open:
  985. dput(parent);
  986. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  987. return 1;
  988. return nfs_lookup_revalidate(dentry, nd);
  989. }
  990. #endif /* CONFIG_NFSV4 */
  991. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  992. {
  993. struct dentry *parent = desc->file->f_path.dentry;
  994. struct inode *dir = parent->d_inode;
  995. struct nfs_entry *entry = desc->entry;
  996. struct dentry *dentry, *alias;
  997. struct qstr name = {
  998. .name = entry->name,
  999. .len = entry->len,
  1000. };
  1001. struct inode *inode;
  1002. unsigned long verf = nfs_save_change_attribute(dir);
  1003. switch (name.len) {
  1004. case 2:
  1005. if (name.name[0] == '.' && name.name[1] == '.')
  1006. return dget_parent(parent);
  1007. break;
  1008. case 1:
  1009. if (name.name[0] == '.')
  1010. return dget(parent);
  1011. }
  1012. spin_lock(&dir->i_lock);
  1013. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1014. spin_unlock(&dir->i_lock);
  1015. return NULL;
  1016. }
  1017. spin_unlock(&dir->i_lock);
  1018. name.hash = full_name_hash(name.name, name.len);
  1019. dentry = d_lookup(parent, &name);
  1020. if (dentry != NULL) {
  1021. /* Is this a positive dentry that matches the readdir info? */
  1022. if (dentry->d_inode != NULL &&
  1023. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1024. d_mountpoint(dentry))) {
  1025. if (!desc->plus || entry->fh->size == 0)
  1026. return dentry;
  1027. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1028. entry->fh) == 0)
  1029. goto out_renew;
  1030. }
  1031. /* No, so d_drop to allow one to be created */
  1032. d_drop(dentry);
  1033. dput(dentry);
  1034. }
  1035. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1036. return NULL;
  1037. if (name.len > NFS_SERVER(dir)->namelen)
  1038. return NULL;
  1039. /* Note: caller is already holding the dir->i_mutex! */
  1040. dentry = d_alloc(parent, &name);
  1041. if (dentry == NULL)
  1042. return NULL;
  1043. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1044. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1045. if (IS_ERR(inode)) {
  1046. dput(dentry);
  1047. return NULL;
  1048. }
  1049. alias = d_materialise_unique(dentry, inode);
  1050. if (alias != NULL) {
  1051. dput(dentry);
  1052. if (IS_ERR(alias))
  1053. return NULL;
  1054. dentry = alias;
  1055. }
  1056. out_renew:
  1057. nfs_set_verifier(dentry, verf);
  1058. return dentry;
  1059. }
  1060. /*
  1061. * Code common to create, mkdir, and mknod.
  1062. */
  1063. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1064. struct nfs_fattr *fattr)
  1065. {
  1066. struct dentry *parent = dget_parent(dentry);
  1067. struct inode *dir = parent->d_inode;
  1068. struct inode *inode;
  1069. int error = -EACCES;
  1070. d_drop(dentry);
  1071. /* We may have been initialized further down */
  1072. if (dentry->d_inode)
  1073. goto out;
  1074. if (fhandle->size == 0) {
  1075. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1076. if (error)
  1077. goto out_error;
  1078. }
  1079. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1080. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1081. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1082. if (error < 0)
  1083. goto out_error;
  1084. }
  1085. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1086. error = PTR_ERR(inode);
  1087. if (IS_ERR(inode))
  1088. goto out_error;
  1089. d_add(dentry, inode);
  1090. out:
  1091. dput(parent);
  1092. return 0;
  1093. out_error:
  1094. nfs_mark_for_revalidate(dir);
  1095. dput(parent);
  1096. return error;
  1097. }
  1098. /*
  1099. * Following a failed create operation, we drop the dentry rather
  1100. * than retain a negative dentry. This avoids a problem in the event
  1101. * that the operation succeeded on the server, but an error in the
  1102. * reply path made it appear to have failed.
  1103. */
  1104. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1105. struct nameidata *nd)
  1106. {
  1107. struct iattr attr;
  1108. int error;
  1109. int open_flags = 0;
  1110. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1111. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1112. attr.ia_mode = mode;
  1113. attr.ia_valid = ATTR_MODE;
  1114. if ((nd->flags & LOOKUP_CREATE) != 0)
  1115. open_flags = nd->intent.open.flags;
  1116. lock_kernel();
  1117. nfs_begin_data_update(dir);
  1118. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1119. nfs_end_data_update(dir);
  1120. if (error != 0)
  1121. goto out_err;
  1122. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1123. unlock_kernel();
  1124. return 0;
  1125. out_err:
  1126. unlock_kernel();
  1127. d_drop(dentry);
  1128. return error;
  1129. }
  1130. /*
  1131. * See comments for nfs_proc_create regarding failed operations.
  1132. */
  1133. static int
  1134. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1135. {
  1136. struct iattr attr;
  1137. int status;
  1138. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1139. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1140. if (!new_valid_dev(rdev))
  1141. return -EINVAL;
  1142. attr.ia_mode = mode;
  1143. attr.ia_valid = ATTR_MODE;
  1144. lock_kernel();
  1145. nfs_begin_data_update(dir);
  1146. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1147. nfs_end_data_update(dir);
  1148. if (status != 0)
  1149. goto out_err;
  1150. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1151. unlock_kernel();
  1152. return 0;
  1153. out_err:
  1154. unlock_kernel();
  1155. d_drop(dentry);
  1156. return status;
  1157. }
  1158. /*
  1159. * See comments for nfs_proc_create regarding failed operations.
  1160. */
  1161. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1162. {
  1163. struct iattr attr;
  1164. int error;
  1165. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1166. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1167. attr.ia_valid = ATTR_MODE;
  1168. attr.ia_mode = mode | S_IFDIR;
  1169. lock_kernel();
  1170. nfs_begin_data_update(dir);
  1171. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1172. nfs_end_data_update(dir);
  1173. if (error != 0)
  1174. goto out_err;
  1175. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1176. unlock_kernel();
  1177. return 0;
  1178. out_err:
  1179. d_drop(dentry);
  1180. unlock_kernel();
  1181. return error;
  1182. }
  1183. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1184. {
  1185. int error;
  1186. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1187. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1188. lock_kernel();
  1189. nfs_begin_data_update(dir);
  1190. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1191. /* Ensure the VFS deletes this inode */
  1192. if (error == 0 && dentry->d_inode != NULL)
  1193. clear_nlink(dentry->d_inode);
  1194. nfs_end_data_update(dir);
  1195. unlock_kernel();
  1196. return error;
  1197. }
  1198. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1199. {
  1200. static unsigned int sillycounter;
  1201. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1202. const int countersize = sizeof(sillycounter)*2;
  1203. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1204. char silly[slen+1];
  1205. struct qstr qsilly;
  1206. struct dentry *sdentry;
  1207. int error = -EIO;
  1208. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1209. dentry->d_parent->d_name.name, dentry->d_name.name,
  1210. atomic_read(&dentry->d_count));
  1211. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1212. /*
  1213. * We don't allow a dentry to be silly-renamed twice.
  1214. */
  1215. error = -EBUSY;
  1216. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1217. goto out;
  1218. sprintf(silly, ".nfs%*.*Lx",
  1219. fileidsize, fileidsize,
  1220. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1221. /* Return delegation in anticipation of the rename */
  1222. nfs_inode_return_delegation(dentry->d_inode);
  1223. sdentry = NULL;
  1224. do {
  1225. char *suffix = silly + slen - countersize;
  1226. dput(sdentry);
  1227. sillycounter++;
  1228. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1229. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1230. dentry->d_name.name, silly);
  1231. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1232. /*
  1233. * N.B. Better to return EBUSY here ... it could be
  1234. * dangerous to delete the file while it's in use.
  1235. */
  1236. if (IS_ERR(sdentry))
  1237. goto out;
  1238. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1239. qsilly.name = silly;
  1240. qsilly.len = strlen(silly);
  1241. nfs_begin_data_update(dir);
  1242. if (dentry->d_inode) {
  1243. nfs_begin_data_update(dentry->d_inode);
  1244. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1245. dir, &qsilly);
  1246. nfs_mark_for_revalidate(dentry->d_inode);
  1247. nfs_end_data_update(dentry->d_inode);
  1248. } else
  1249. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1250. dir, &qsilly);
  1251. nfs_end_data_update(dir);
  1252. if (!error) {
  1253. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1254. d_move(dentry, sdentry);
  1255. error = nfs_async_unlink(dir, dentry);
  1256. /* If we return 0 we don't unlink */
  1257. }
  1258. dput(sdentry);
  1259. out:
  1260. return error;
  1261. }
  1262. /*
  1263. * Remove a file after making sure there are no pending writes,
  1264. * and after checking that the file has only one user.
  1265. *
  1266. * We invalidate the attribute cache and free the inode prior to the operation
  1267. * to avoid possible races if the server reuses the inode.
  1268. */
  1269. static int nfs_safe_remove(struct dentry *dentry)
  1270. {
  1271. struct inode *dir = dentry->d_parent->d_inode;
  1272. struct inode *inode = dentry->d_inode;
  1273. int error = -EBUSY;
  1274. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1275. dentry->d_parent->d_name.name, dentry->d_name.name);
  1276. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1277. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1278. error = 0;
  1279. goto out;
  1280. }
  1281. nfs_begin_data_update(dir);
  1282. if (inode != NULL) {
  1283. nfs_inode_return_delegation(inode);
  1284. nfs_begin_data_update(inode);
  1285. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1286. /* The VFS may want to delete this inode */
  1287. if (error == 0)
  1288. drop_nlink(inode);
  1289. nfs_mark_for_revalidate(inode);
  1290. nfs_end_data_update(inode);
  1291. } else
  1292. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1293. nfs_end_data_update(dir);
  1294. out:
  1295. return error;
  1296. }
  1297. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1298. * belongs to an active ".nfs..." file and we return -EBUSY.
  1299. *
  1300. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1301. */
  1302. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1303. {
  1304. int error;
  1305. int need_rehash = 0;
  1306. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1307. dir->i_ino, dentry->d_name.name);
  1308. lock_kernel();
  1309. spin_lock(&dcache_lock);
  1310. spin_lock(&dentry->d_lock);
  1311. if (atomic_read(&dentry->d_count) > 1) {
  1312. spin_unlock(&dentry->d_lock);
  1313. spin_unlock(&dcache_lock);
  1314. /* Start asynchronous writeout of the inode */
  1315. write_inode_now(dentry->d_inode, 0);
  1316. error = nfs_sillyrename(dir, dentry);
  1317. unlock_kernel();
  1318. return error;
  1319. }
  1320. if (!d_unhashed(dentry)) {
  1321. __d_drop(dentry);
  1322. need_rehash = 1;
  1323. }
  1324. spin_unlock(&dentry->d_lock);
  1325. spin_unlock(&dcache_lock);
  1326. error = nfs_safe_remove(dentry);
  1327. if (!error) {
  1328. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1329. } else if (need_rehash)
  1330. d_rehash(dentry);
  1331. unlock_kernel();
  1332. return error;
  1333. }
  1334. /*
  1335. * To create a symbolic link, most file systems instantiate a new inode,
  1336. * add a page to it containing the path, then write it out to the disk
  1337. * using prepare_write/commit_write.
  1338. *
  1339. * Unfortunately the NFS client can't create the in-core inode first
  1340. * because it needs a file handle to create an in-core inode (see
  1341. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1342. * symlink request has completed on the server.
  1343. *
  1344. * So instead we allocate a raw page, copy the symname into it, then do
  1345. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1346. * now have a new file handle and can instantiate an in-core NFS inode
  1347. * and move the raw page into its mapping.
  1348. */
  1349. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1350. {
  1351. struct pagevec lru_pvec;
  1352. struct page *page;
  1353. char *kaddr;
  1354. struct iattr attr;
  1355. unsigned int pathlen = strlen(symname);
  1356. int error;
  1357. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1358. dir->i_ino, dentry->d_name.name, symname);
  1359. if (pathlen > PAGE_SIZE)
  1360. return -ENAMETOOLONG;
  1361. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1362. attr.ia_valid = ATTR_MODE;
  1363. lock_kernel();
  1364. page = alloc_page(GFP_HIGHUSER);
  1365. if (!page) {
  1366. unlock_kernel();
  1367. return -ENOMEM;
  1368. }
  1369. kaddr = kmap_atomic(page, KM_USER0);
  1370. memcpy(kaddr, symname, pathlen);
  1371. if (pathlen < PAGE_SIZE)
  1372. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1373. kunmap_atomic(kaddr, KM_USER0);
  1374. nfs_begin_data_update(dir);
  1375. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1376. nfs_end_data_update(dir);
  1377. if (error != 0) {
  1378. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1379. dir->i_sb->s_id, dir->i_ino,
  1380. dentry->d_name.name, symname, error);
  1381. d_drop(dentry);
  1382. __free_page(page);
  1383. unlock_kernel();
  1384. return error;
  1385. }
  1386. /*
  1387. * No big deal if we can't add this page to the page cache here.
  1388. * READLINK will get the missing page from the server if needed.
  1389. */
  1390. pagevec_init(&lru_pvec, 0);
  1391. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1392. GFP_KERNEL)) {
  1393. pagevec_add(&lru_pvec, page);
  1394. pagevec_lru_add(&lru_pvec);
  1395. SetPageUptodate(page);
  1396. unlock_page(page);
  1397. } else
  1398. __free_page(page);
  1399. unlock_kernel();
  1400. return 0;
  1401. }
  1402. static int
  1403. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1404. {
  1405. struct inode *inode = old_dentry->d_inode;
  1406. int error;
  1407. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1408. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1409. dentry->d_parent->d_name.name, dentry->d_name.name);
  1410. lock_kernel();
  1411. nfs_begin_data_update(dir);
  1412. nfs_begin_data_update(inode);
  1413. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1414. if (error == 0) {
  1415. atomic_inc(&inode->i_count);
  1416. d_instantiate(dentry, inode);
  1417. }
  1418. nfs_end_data_update(inode);
  1419. nfs_end_data_update(dir);
  1420. unlock_kernel();
  1421. return error;
  1422. }
  1423. /*
  1424. * RENAME
  1425. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1426. * different file handle for the same inode after a rename (e.g. when
  1427. * moving to a different directory). A fail-safe method to do so would
  1428. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1429. * rename the old file using the sillyrename stuff. This way, the original
  1430. * file in old_dir will go away when the last process iput()s the inode.
  1431. *
  1432. * FIXED.
  1433. *
  1434. * It actually works quite well. One needs to have the possibility for
  1435. * at least one ".nfs..." file in each directory the file ever gets
  1436. * moved or linked to which happens automagically with the new
  1437. * implementation that only depends on the dcache stuff instead of
  1438. * using the inode layer
  1439. *
  1440. * Unfortunately, things are a little more complicated than indicated
  1441. * above. For a cross-directory move, we want to make sure we can get
  1442. * rid of the old inode after the operation. This means there must be
  1443. * no pending writes (if it's a file), and the use count must be 1.
  1444. * If these conditions are met, we can drop the dentries before doing
  1445. * the rename.
  1446. */
  1447. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1448. struct inode *new_dir, struct dentry *new_dentry)
  1449. {
  1450. struct inode *old_inode = old_dentry->d_inode;
  1451. struct inode *new_inode = new_dentry->d_inode;
  1452. struct dentry *dentry = NULL, *rehash = NULL;
  1453. int error = -EBUSY;
  1454. /*
  1455. * To prevent any new references to the target during the rename,
  1456. * we unhash the dentry and free the inode in advance.
  1457. */
  1458. lock_kernel();
  1459. if (!d_unhashed(new_dentry)) {
  1460. d_drop(new_dentry);
  1461. rehash = new_dentry;
  1462. }
  1463. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1464. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1465. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1466. atomic_read(&new_dentry->d_count));
  1467. /*
  1468. * First check whether the target is busy ... we can't
  1469. * safely do _any_ rename if the target is in use.
  1470. *
  1471. * For files, make a copy of the dentry and then do a
  1472. * silly-rename. If the silly-rename succeeds, the
  1473. * copied dentry is hashed and becomes the new target.
  1474. */
  1475. if (!new_inode)
  1476. goto go_ahead;
  1477. if (S_ISDIR(new_inode->i_mode)) {
  1478. error = -EISDIR;
  1479. if (!S_ISDIR(old_inode->i_mode))
  1480. goto out;
  1481. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1482. int err;
  1483. /* copy the target dentry's name */
  1484. dentry = d_alloc(new_dentry->d_parent,
  1485. &new_dentry->d_name);
  1486. if (!dentry)
  1487. goto out;
  1488. /* silly-rename the existing target ... */
  1489. err = nfs_sillyrename(new_dir, new_dentry);
  1490. if (!err) {
  1491. new_dentry = rehash = dentry;
  1492. new_inode = NULL;
  1493. /* instantiate the replacement target */
  1494. d_instantiate(new_dentry, NULL);
  1495. } else if (atomic_read(&new_dentry->d_count) > 1)
  1496. /* dentry still busy? */
  1497. goto out;
  1498. } else
  1499. drop_nlink(new_inode);
  1500. go_ahead:
  1501. /*
  1502. * ... prune child dentries and writebacks if needed.
  1503. */
  1504. if (atomic_read(&old_dentry->d_count) > 1) {
  1505. if (S_ISREG(old_inode->i_mode))
  1506. nfs_wb_all(old_inode);
  1507. shrink_dcache_parent(old_dentry);
  1508. }
  1509. nfs_inode_return_delegation(old_inode);
  1510. if (new_inode != NULL) {
  1511. nfs_inode_return_delegation(new_inode);
  1512. d_delete(new_dentry);
  1513. }
  1514. nfs_begin_data_update(old_dir);
  1515. nfs_begin_data_update(new_dir);
  1516. nfs_begin_data_update(old_inode);
  1517. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1518. new_dir, &new_dentry->d_name);
  1519. nfs_mark_for_revalidate(old_inode);
  1520. nfs_end_data_update(old_inode);
  1521. nfs_end_data_update(new_dir);
  1522. nfs_end_data_update(old_dir);
  1523. out:
  1524. if (rehash)
  1525. d_rehash(rehash);
  1526. if (!error) {
  1527. d_move(old_dentry, new_dentry);
  1528. nfs_set_verifier(new_dentry,
  1529. nfs_save_change_attribute(new_dir));
  1530. }
  1531. /* new dentry created? */
  1532. if (dentry)
  1533. dput(dentry);
  1534. unlock_kernel();
  1535. return error;
  1536. }
  1537. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1538. static LIST_HEAD(nfs_access_lru_list);
  1539. static atomic_long_t nfs_access_nr_entries;
  1540. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1541. {
  1542. put_rpccred(entry->cred);
  1543. kfree(entry);
  1544. smp_mb__before_atomic_dec();
  1545. atomic_long_dec(&nfs_access_nr_entries);
  1546. smp_mb__after_atomic_dec();
  1547. }
  1548. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1549. {
  1550. LIST_HEAD(head);
  1551. struct nfs_inode *nfsi;
  1552. struct nfs_access_entry *cache;
  1553. restart:
  1554. spin_lock(&nfs_access_lru_lock);
  1555. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1556. struct inode *inode;
  1557. if (nr_to_scan-- == 0)
  1558. break;
  1559. inode = igrab(&nfsi->vfs_inode);
  1560. if (inode == NULL)
  1561. continue;
  1562. spin_lock(&inode->i_lock);
  1563. if (list_empty(&nfsi->access_cache_entry_lru))
  1564. goto remove_lru_entry;
  1565. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1566. struct nfs_access_entry, lru);
  1567. list_move(&cache->lru, &head);
  1568. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1569. if (!list_empty(&nfsi->access_cache_entry_lru))
  1570. list_move_tail(&nfsi->access_cache_inode_lru,
  1571. &nfs_access_lru_list);
  1572. else {
  1573. remove_lru_entry:
  1574. list_del_init(&nfsi->access_cache_inode_lru);
  1575. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1576. }
  1577. spin_unlock(&inode->i_lock);
  1578. spin_unlock(&nfs_access_lru_lock);
  1579. iput(inode);
  1580. goto restart;
  1581. }
  1582. spin_unlock(&nfs_access_lru_lock);
  1583. while (!list_empty(&head)) {
  1584. cache = list_entry(head.next, struct nfs_access_entry, lru);
  1585. list_del(&cache->lru);
  1586. nfs_access_free_entry(cache);
  1587. }
  1588. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1589. }
  1590. static void __nfs_access_zap_cache(struct inode *inode)
  1591. {
  1592. struct nfs_inode *nfsi = NFS_I(inode);
  1593. struct rb_root *root_node = &nfsi->access_cache;
  1594. struct rb_node *n, *dispose = NULL;
  1595. struct nfs_access_entry *entry;
  1596. /* Unhook entries from the cache */
  1597. while ((n = rb_first(root_node)) != NULL) {
  1598. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1599. rb_erase(n, root_node);
  1600. list_del(&entry->lru);
  1601. n->rb_left = dispose;
  1602. dispose = n;
  1603. }
  1604. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1605. spin_unlock(&inode->i_lock);
  1606. /* Now kill them all! */
  1607. while (dispose != NULL) {
  1608. n = dispose;
  1609. dispose = n->rb_left;
  1610. nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
  1611. }
  1612. }
  1613. void nfs_access_zap_cache(struct inode *inode)
  1614. {
  1615. /* Remove from global LRU init */
  1616. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1617. spin_lock(&nfs_access_lru_lock);
  1618. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1619. spin_unlock(&nfs_access_lru_lock);
  1620. }
  1621. spin_lock(&inode->i_lock);
  1622. /* This will release the spinlock */
  1623. __nfs_access_zap_cache(inode);
  1624. }
  1625. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1626. {
  1627. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1628. struct nfs_access_entry *entry;
  1629. while (n != NULL) {
  1630. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1631. if (cred < entry->cred)
  1632. n = n->rb_left;
  1633. else if (cred > entry->cred)
  1634. n = n->rb_right;
  1635. else
  1636. return entry;
  1637. }
  1638. return NULL;
  1639. }
  1640. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1641. {
  1642. struct nfs_inode *nfsi = NFS_I(inode);
  1643. struct nfs_access_entry *cache;
  1644. int err = -ENOENT;
  1645. spin_lock(&inode->i_lock);
  1646. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1647. goto out_zap;
  1648. cache = nfs_access_search_rbtree(inode, cred);
  1649. if (cache == NULL)
  1650. goto out;
  1651. if (!time_in_range(jiffies, cache->jiffies, cache->jiffies + NFS_ATTRTIMEO(inode)))
  1652. goto out_stale;
  1653. res->jiffies = cache->jiffies;
  1654. res->cred = cache->cred;
  1655. res->mask = cache->mask;
  1656. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1657. err = 0;
  1658. out:
  1659. spin_unlock(&inode->i_lock);
  1660. return err;
  1661. out_stale:
  1662. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1663. list_del(&cache->lru);
  1664. spin_unlock(&inode->i_lock);
  1665. nfs_access_free_entry(cache);
  1666. return -ENOENT;
  1667. out_zap:
  1668. /* This will release the spinlock */
  1669. __nfs_access_zap_cache(inode);
  1670. return -ENOENT;
  1671. }
  1672. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1673. {
  1674. struct nfs_inode *nfsi = NFS_I(inode);
  1675. struct rb_root *root_node = &nfsi->access_cache;
  1676. struct rb_node **p = &root_node->rb_node;
  1677. struct rb_node *parent = NULL;
  1678. struct nfs_access_entry *entry;
  1679. spin_lock(&inode->i_lock);
  1680. while (*p != NULL) {
  1681. parent = *p;
  1682. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1683. if (set->cred < entry->cred)
  1684. p = &parent->rb_left;
  1685. else if (set->cred > entry->cred)
  1686. p = &parent->rb_right;
  1687. else
  1688. goto found;
  1689. }
  1690. rb_link_node(&set->rb_node, parent, p);
  1691. rb_insert_color(&set->rb_node, root_node);
  1692. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1693. spin_unlock(&inode->i_lock);
  1694. return;
  1695. found:
  1696. rb_replace_node(parent, &set->rb_node, root_node);
  1697. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1698. list_del(&entry->lru);
  1699. spin_unlock(&inode->i_lock);
  1700. nfs_access_free_entry(entry);
  1701. }
  1702. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1703. {
  1704. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1705. if (cache == NULL)
  1706. return;
  1707. RB_CLEAR_NODE(&cache->rb_node);
  1708. cache->jiffies = set->jiffies;
  1709. cache->cred = get_rpccred(set->cred);
  1710. cache->mask = set->mask;
  1711. nfs_access_add_rbtree(inode, cache);
  1712. /* Update accounting */
  1713. smp_mb__before_atomic_inc();
  1714. atomic_long_inc(&nfs_access_nr_entries);
  1715. smp_mb__after_atomic_inc();
  1716. /* Add inode to global LRU list */
  1717. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_FLAGS(inode))) {
  1718. spin_lock(&nfs_access_lru_lock);
  1719. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1720. spin_unlock(&nfs_access_lru_lock);
  1721. }
  1722. }
  1723. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1724. {
  1725. struct nfs_access_entry cache;
  1726. int status;
  1727. status = nfs_access_get_cached(inode, cred, &cache);
  1728. if (status == 0)
  1729. goto out;
  1730. /* Be clever: ask server to check for all possible rights */
  1731. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1732. cache.cred = cred;
  1733. cache.jiffies = jiffies;
  1734. status = NFS_PROTO(inode)->access(inode, &cache);
  1735. if (status != 0)
  1736. return status;
  1737. nfs_access_add_cache(inode, &cache);
  1738. out:
  1739. if ((cache.mask & mask) == mask)
  1740. return 0;
  1741. return -EACCES;
  1742. }
  1743. static int nfs_open_permission_mask(int openflags)
  1744. {
  1745. int mask = 0;
  1746. if (openflags & FMODE_READ)
  1747. mask |= MAY_READ;
  1748. if (openflags & FMODE_WRITE)
  1749. mask |= MAY_WRITE;
  1750. if (openflags & FMODE_EXEC)
  1751. mask |= MAY_EXEC;
  1752. return mask;
  1753. }
  1754. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1755. {
  1756. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1757. }
  1758. int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1759. {
  1760. struct rpc_cred *cred;
  1761. int res = 0;
  1762. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1763. if (mask == 0)
  1764. goto out;
  1765. /* Is this sys_access() ? */
  1766. if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
  1767. goto force_lookup;
  1768. switch (inode->i_mode & S_IFMT) {
  1769. case S_IFLNK:
  1770. goto out;
  1771. case S_IFREG:
  1772. /* NFSv4 has atomic_open... */
  1773. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1774. && nd != NULL
  1775. && (nd->flags & LOOKUP_OPEN))
  1776. goto out;
  1777. break;
  1778. case S_IFDIR:
  1779. /*
  1780. * Optimize away all write operations, since the server
  1781. * will check permissions when we perform the op.
  1782. */
  1783. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1784. goto out;
  1785. }
  1786. force_lookup:
  1787. lock_kernel();
  1788. if (!NFS_PROTO(inode)->access)
  1789. goto out_notsup;
  1790. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1791. if (!IS_ERR(cred)) {
  1792. res = nfs_do_access(inode, cred, mask);
  1793. put_rpccred(cred);
  1794. } else
  1795. res = PTR_ERR(cred);
  1796. unlock_kernel();
  1797. out:
  1798. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1799. inode->i_sb->s_id, inode->i_ino, mask, res);
  1800. return res;
  1801. out_notsup:
  1802. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1803. if (res == 0)
  1804. res = generic_permission(inode, mask, NULL);
  1805. unlock_kernel();
  1806. goto out;
  1807. }
  1808. /*
  1809. * Local variables:
  1810. * version-control: t
  1811. * kept-new-versions: 5
  1812. * End:
  1813. */