dir.c 53 KB

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