dir.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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/namei.h>
  33. #include "nfs4_fs.h"
  34. #include "delegation.h"
  35. #define NFS_PARANOIA 1
  36. /* #define NFS_DEBUG_VERBOSE 1 */
  37. static int nfs_opendir(struct inode *, struct file *);
  38. static int nfs_readdir(struct file *, void *, filldir_t);
  39. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  40. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  41. static int nfs_mkdir(struct inode *, struct dentry *, int);
  42. static int nfs_rmdir(struct inode *, struct dentry *);
  43. static int nfs_unlink(struct inode *, struct dentry *);
  44. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  45. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  46. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  47. static int nfs_rename(struct inode *, struct dentry *,
  48. struct inode *, struct dentry *);
  49. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  50. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  51. struct file_operations nfs_dir_operations = {
  52. .llseek = nfs_llseek_dir,
  53. .read = generic_read_dir,
  54. .readdir = nfs_readdir,
  55. .open = nfs_opendir,
  56. .release = nfs_release,
  57. .fsync = nfs_fsync_dir,
  58. };
  59. struct inode_operations nfs_dir_inode_operations = {
  60. .create = nfs_create,
  61. .lookup = nfs_lookup,
  62. .link = nfs_link,
  63. .unlink = nfs_unlink,
  64. .symlink = nfs_symlink,
  65. .mkdir = nfs_mkdir,
  66. .rmdir = nfs_rmdir,
  67. .mknod = nfs_mknod,
  68. .rename = nfs_rename,
  69. .permission = nfs_permission,
  70. .getattr = nfs_getattr,
  71. .setattr = nfs_setattr,
  72. };
  73. #ifdef CONFIG_NFS_V3
  74. struct inode_operations nfs3_dir_inode_operations = {
  75. .create = nfs_create,
  76. .lookup = nfs_lookup,
  77. .link = nfs_link,
  78. .unlink = nfs_unlink,
  79. .symlink = nfs_symlink,
  80. .mkdir = nfs_mkdir,
  81. .rmdir = nfs_rmdir,
  82. .mknod = nfs_mknod,
  83. .rename = nfs_rename,
  84. .permission = nfs_permission,
  85. .getattr = nfs_getattr,
  86. .setattr = nfs_setattr,
  87. .listxattr = nfs3_listxattr,
  88. .getxattr = nfs3_getxattr,
  89. .setxattr = nfs3_setxattr,
  90. .removexattr = nfs3_removexattr,
  91. };
  92. #endif /* CONFIG_NFS_V3 */
  93. #ifdef CONFIG_NFS_V4
  94. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  95. struct inode_operations nfs4_dir_inode_operations = {
  96. .create = nfs_create,
  97. .lookup = nfs_atomic_lookup,
  98. .link = nfs_link,
  99. .unlink = nfs_unlink,
  100. .symlink = nfs_symlink,
  101. .mkdir = nfs_mkdir,
  102. .rmdir = nfs_rmdir,
  103. .mknod = nfs_mknod,
  104. .rename = nfs_rename,
  105. .permission = nfs_permission,
  106. .getattr = nfs_getattr,
  107. .setattr = nfs_setattr,
  108. .getxattr = nfs4_getxattr,
  109. .setxattr = nfs4_setxattr,
  110. .listxattr = nfs4_listxattr,
  111. };
  112. #endif /* CONFIG_NFS_V4 */
  113. /*
  114. * Open file
  115. */
  116. static int
  117. nfs_opendir(struct inode *inode, struct file *filp)
  118. {
  119. int res = 0;
  120. lock_kernel();
  121. /* Call generic open code in order to cache credentials */
  122. if (!res)
  123. res = nfs_open(inode, filp);
  124. unlock_kernel();
  125. return res;
  126. }
  127. typedef u32 * (*decode_dirent_t)(u32 *, struct nfs_entry *, int);
  128. typedef struct {
  129. struct file *file;
  130. struct page *page;
  131. unsigned long page_index;
  132. u32 *ptr;
  133. u64 *dir_cookie;
  134. loff_t current_index;
  135. struct nfs_entry *entry;
  136. decode_dirent_t decode;
  137. int plus;
  138. int error;
  139. } nfs_readdir_descriptor_t;
  140. /* Now we cache directories properly, by stuffing the dirent
  141. * data directly in the page cache.
  142. *
  143. * Inode invalidation due to refresh etc. takes care of
  144. * _everything_, no sloppy entry flushing logic, no extraneous
  145. * copying, network direct to page cache, the way it was meant
  146. * to be.
  147. *
  148. * NOTE: Dirent information verification is done always by the
  149. * page-in of the RPC reply, nowhere else, this simplies
  150. * things substantially.
  151. */
  152. static
  153. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  154. {
  155. struct file *file = desc->file;
  156. struct inode *inode = file->f_dentry->d_inode;
  157. struct rpc_cred *cred = nfs_file_cred(file);
  158. unsigned long timestamp;
  159. int error;
  160. dfprintk(VFS, "NFS: nfs_readdir_filler() reading cookie %Lu into page %lu.\n", (long long)desc->entry->cookie, page->index);
  161. again:
  162. timestamp = jiffies;
  163. error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, desc->entry->cookie, page,
  164. NFS_SERVER(inode)->dtsize, desc->plus);
  165. if (error < 0) {
  166. /* We requested READDIRPLUS, but the server doesn't grok it */
  167. if (error == -ENOTSUPP && desc->plus) {
  168. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  169. NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
  170. desc->plus = 0;
  171. goto again;
  172. }
  173. goto error;
  174. }
  175. SetPageUptodate(page);
  176. NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
  177. /* Ensure consistent page alignment of the data.
  178. * Note: assumes we have exclusive access to this mapping either
  179. * through inode->i_sem or some other mechanism.
  180. */
  181. if (page->index == 0)
  182. invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1);
  183. unlock_page(page);
  184. return 0;
  185. error:
  186. SetPageError(page);
  187. unlock_page(page);
  188. nfs_zap_caches(inode);
  189. desc->error = error;
  190. return -EIO;
  191. }
  192. static inline
  193. int dir_decode(nfs_readdir_descriptor_t *desc)
  194. {
  195. u32 *p = desc->ptr;
  196. p = desc->decode(p, desc->entry, desc->plus);
  197. if (IS_ERR(p))
  198. return PTR_ERR(p);
  199. desc->ptr = p;
  200. return 0;
  201. }
  202. static inline
  203. void dir_page_release(nfs_readdir_descriptor_t *desc)
  204. {
  205. kunmap(desc->page);
  206. page_cache_release(desc->page);
  207. desc->page = NULL;
  208. desc->ptr = NULL;
  209. }
  210. /*
  211. * Given a pointer to a buffer that has already been filled by a call
  212. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  213. *
  214. * If the end of the buffer has been reached, return -EAGAIN, if not,
  215. * return the offset within the buffer of the next entry to be
  216. * read.
  217. */
  218. static inline
  219. int find_dirent(nfs_readdir_descriptor_t *desc)
  220. {
  221. struct nfs_entry *entry = desc->entry;
  222. int loop_count = 0,
  223. status;
  224. while((status = dir_decode(desc)) == 0) {
  225. dfprintk(VFS, "NFS: found cookie %Lu\n", (unsigned long long)entry->cookie);
  226. if (entry->prev_cookie == *desc->dir_cookie)
  227. break;
  228. if (loop_count++ > 200) {
  229. loop_count = 0;
  230. schedule();
  231. }
  232. }
  233. dfprintk(VFS, "NFS: find_dirent() returns %d\n", status);
  234. return status;
  235. }
  236. /*
  237. * Given a pointer to a buffer that has already been filled by a call
  238. * to readdir, find the entry at offset 'desc->file->f_pos'.
  239. *
  240. * If the end of the buffer has been reached, return -EAGAIN, if not,
  241. * return the offset within the buffer of the next entry to be
  242. * read.
  243. */
  244. static inline
  245. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  246. {
  247. struct nfs_entry *entry = desc->entry;
  248. int loop_count = 0,
  249. status;
  250. for(;;) {
  251. status = dir_decode(desc);
  252. if (status)
  253. break;
  254. dfprintk(VFS, "NFS: found cookie %Lu at index %Ld\n", (unsigned long long)entry->cookie, desc->current_index);
  255. if (desc->file->f_pos == desc->current_index) {
  256. *desc->dir_cookie = entry->cookie;
  257. break;
  258. }
  259. desc->current_index++;
  260. if (loop_count++ > 200) {
  261. loop_count = 0;
  262. schedule();
  263. }
  264. }
  265. dfprintk(VFS, "NFS: find_dirent_index() returns %d\n", status);
  266. return status;
  267. }
  268. /*
  269. * Find the given page, and call find_dirent() or find_dirent_index in
  270. * order to try to return the next entry.
  271. */
  272. static inline
  273. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  274. {
  275. struct inode *inode = desc->file->f_dentry->d_inode;
  276. struct page *page;
  277. int status;
  278. dfprintk(VFS, "NFS: find_dirent_page() searching directory page %ld\n", desc->page_index);
  279. page = read_cache_page(inode->i_mapping, desc->page_index,
  280. (filler_t *)nfs_readdir_filler, desc);
  281. if (IS_ERR(page)) {
  282. status = PTR_ERR(page);
  283. goto out;
  284. }
  285. if (!PageUptodate(page))
  286. goto read_error;
  287. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  288. desc->page = page;
  289. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  290. if (*desc->dir_cookie != 0)
  291. status = find_dirent(desc);
  292. else
  293. status = find_dirent_index(desc);
  294. if (status < 0)
  295. dir_page_release(desc);
  296. out:
  297. dfprintk(VFS, "NFS: find_dirent_page() returns %d\n", status);
  298. return status;
  299. read_error:
  300. page_cache_release(page);
  301. return -EIO;
  302. }
  303. /*
  304. * Recurse through the page cache pages, and return a
  305. * filled nfs_entry structure of the next directory entry if possible.
  306. *
  307. * The target for the search is '*desc->dir_cookie' if non-0,
  308. * 'desc->file->f_pos' otherwise
  309. */
  310. static inline
  311. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  312. {
  313. int loop_count = 0;
  314. int res;
  315. /* Always search-by-index from the beginning of the cache */
  316. if (*desc->dir_cookie == 0) {
  317. dfprintk(VFS, "NFS: readdir_search_pagecache() searching for offset %Ld\n", (long long)desc->file->f_pos);
  318. desc->page_index = 0;
  319. desc->entry->cookie = desc->entry->prev_cookie = 0;
  320. desc->entry->eof = 0;
  321. desc->current_index = 0;
  322. } else
  323. dfprintk(VFS, "NFS: readdir_search_pagecache() searching for cookie %Lu\n", (unsigned long long)*desc->dir_cookie);
  324. for (;;) {
  325. res = find_dirent_page(desc);
  326. if (res != -EAGAIN)
  327. break;
  328. /* Align to beginning of next page */
  329. desc->page_index ++;
  330. if (loop_count++ > 200) {
  331. loop_count = 0;
  332. schedule();
  333. }
  334. }
  335. dfprintk(VFS, "NFS: readdir_search_pagecache() returned %d\n", res);
  336. return res;
  337. }
  338. static inline unsigned int dt_type(struct inode *inode)
  339. {
  340. return (inode->i_mode >> 12) & 15;
  341. }
  342. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  343. /*
  344. * Once we've found the start of the dirent within a page: fill 'er up...
  345. */
  346. static
  347. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  348. filldir_t filldir)
  349. {
  350. struct file *file = desc->file;
  351. struct nfs_entry *entry = desc->entry;
  352. struct dentry *dentry = NULL;
  353. unsigned long fileid;
  354. int loop_count = 0,
  355. res;
  356. dfprintk(VFS, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n", (long long)entry->cookie);
  357. for(;;) {
  358. unsigned d_type = DT_UNKNOWN;
  359. /* Note: entry->prev_cookie contains the cookie for
  360. * retrieving the current dirent on the server */
  361. fileid = nfs_fileid_to_ino_t(entry->ino);
  362. /* Get a dentry if we have one */
  363. if (dentry != NULL)
  364. dput(dentry);
  365. dentry = nfs_readdir_lookup(desc);
  366. /* Use readdirplus info */
  367. if (dentry != NULL && dentry->d_inode != NULL) {
  368. d_type = dt_type(dentry->d_inode);
  369. fileid = dentry->d_inode->i_ino;
  370. }
  371. res = filldir(dirent, entry->name, entry->len,
  372. file->f_pos, fileid, d_type);
  373. if (res < 0)
  374. break;
  375. file->f_pos++;
  376. *desc->dir_cookie = entry->cookie;
  377. if (dir_decode(desc) != 0) {
  378. desc->page_index ++;
  379. break;
  380. }
  381. if (loop_count++ > 200) {
  382. loop_count = 0;
  383. schedule();
  384. }
  385. }
  386. dir_page_release(desc);
  387. if (dentry != NULL)
  388. dput(dentry);
  389. dfprintk(VFS, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n", (unsigned long long)*desc->dir_cookie, res);
  390. return res;
  391. }
  392. /*
  393. * If we cannot find a cookie in our cache, we suspect that this is
  394. * because it points to a deleted file, so we ask the server to return
  395. * whatever it thinks is the next entry. We then feed this to filldir.
  396. * If all goes well, we should then be able to find our way round the
  397. * cache on the next call to readdir_search_pagecache();
  398. *
  399. * NOTE: we cannot add the anonymous page to the pagecache because
  400. * the data it contains might not be page aligned. Besides,
  401. * we should already have a complete representation of the
  402. * directory in the page cache by the time we get here.
  403. */
  404. static inline
  405. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  406. filldir_t filldir)
  407. {
  408. struct file *file = desc->file;
  409. struct inode *inode = file->f_dentry->d_inode;
  410. struct rpc_cred *cred = nfs_file_cred(file);
  411. struct page *page = NULL;
  412. int status;
  413. dfprintk(VFS, "NFS: uncached_readdir() searching for cookie %Lu\n", (unsigned long long)*desc->dir_cookie);
  414. page = alloc_page(GFP_HIGHUSER);
  415. if (!page) {
  416. status = -ENOMEM;
  417. goto out;
  418. }
  419. desc->error = NFS_PROTO(inode)->readdir(file->f_dentry, cred, *desc->dir_cookie,
  420. page,
  421. NFS_SERVER(inode)->dtsize,
  422. desc->plus);
  423. NFS_FLAGS(inode) |= NFS_INO_INVALID_ATIME;
  424. desc->page = page;
  425. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  426. if (desc->error >= 0) {
  427. if ((status = dir_decode(desc)) == 0)
  428. desc->entry->prev_cookie = *desc->dir_cookie;
  429. } else
  430. status = -EIO;
  431. if (status < 0)
  432. goto out_release;
  433. status = nfs_do_filldir(desc, dirent, filldir);
  434. /* Reset read descriptor so it searches the page cache from
  435. * the start upon the next call to readdir_search_pagecache() */
  436. desc->page_index = 0;
  437. desc->entry->cookie = desc->entry->prev_cookie = 0;
  438. desc->entry->eof = 0;
  439. out:
  440. dfprintk(VFS, "NFS: uncached_readdir() returns %d\n", status);
  441. return status;
  442. out_release:
  443. dir_page_release(desc);
  444. goto out;
  445. }
  446. /* The file offset position represents the dirent entry number. A
  447. last cookie cache takes care of the common case of reading the
  448. whole directory.
  449. */
  450. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  451. {
  452. struct dentry *dentry = filp->f_dentry;
  453. struct inode *inode = dentry->d_inode;
  454. nfs_readdir_descriptor_t my_desc,
  455. *desc = &my_desc;
  456. struct nfs_entry my_entry;
  457. struct nfs_fh fh;
  458. struct nfs_fattr fattr;
  459. long res;
  460. lock_kernel();
  461. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  462. if (res < 0) {
  463. unlock_kernel();
  464. return res;
  465. }
  466. /*
  467. * filp->f_pos points to the dirent entry number.
  468. * *desc->dir_cookie has the cookie for the next entry. We have
  469. * to either find the entry with the appropriate number or
  470. * revalidate the cookie.
  471. */
  472. memset(desc, 0, sizeof(*desc));
  473. desc->file = filp;
  474. desc->dir_cookie = &((struct nfs_open_context *)filp->private_data)->dir_cookie;
  475. desc->decode = NFS_PROTO(inode)->decode_dirent;
  476. desc->plus = NFS_USE_READDIRPLUS(inode);
  477. my_entry.cookie = my_entry.prev_cookie = 0;
  478. my_entry.eof = 0;
  479. my_entry.fh = &fh;
  480. my_entry.fattr = &fattr;
  481. desc->entry = &my_entry;
  482. while(!desc->entry->eof) {
  483. res = readdir_search_pagecache(desc);
  484. if (res == -EBADCOOKIE) {
  485. /* This means either end of directory */
  486. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  487. /* Or that the server has 'lost' a cookie */
  488. res = uncached_readdir(desc, dirent, filldir);
  489. if (res >= 0)
  490. continue;
  491. }
  492. res = 0;
  493. break;
  494. }
  495. if (res == -ETOOSMALL && desc->plus) {
  496. NFS_FLAGS(inode) &= ~NFS_INO_ADVISE_RDPLUS;
  497. nfs_zap_caches(inode);
  498. desc->plus = 0;
  499. desc->entry->eof = 0;
  500. continue;
  501. }
  502. if (res < 0)
  503. break;
  504. res = nfs_do_filldir(desc, dirent, filldir);
  505. if (res < 0) {
  506. res = 0;
  507. break;
  508. }
  509. }
  510. unlock_kernel();
  511. if (desc->error < 0)
  512. return desc->error;
  513. if (res < 0)
  514. return res;
  515. return 0;
  516. }
  517. loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  518. {
  519. down(&filp->f_dentry->d_inode->i_sem);
  520. switch (origin) {
  521. case 1:
  522. offset += filp->f_pos;
  523. case 0:
  524. if (offset >= 0)
  525. break;
  526. default:
  527. offset = -EINVAL;
  528. goto out;
  529. }
  530. if (offset != filp->f_pos) {
  531. filp->f_pos = offset;
  532. ((struct nfs_open_context *)filp->private_data)->dir_cookie = 0;
  533. }
  534. out:
  535. up(&filp->f_dentry->d_inode->i_sem);
  536. return offset;
  537. }
  538. /*
  539. * All directory operations under NFS are synchronous, so fsync()
  540. * is a dummy operation.
  541. */
  542. int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  543. {
  544. return 0;
  545. }
  546. /*
  547. * A check for whether or not the parent directory has changed.
  548. * In the case it has, we assume that the dentries are untrustworthy
  549. * and may need to be looked up again.
  550. */
  551. static inline int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  552. {
  553. if (IS_ROOT(dentry))
  554. return 1;
  555. if ((NFS_FLAGS(dir) & NFS_INO_INVALID_ATTR) != 0
  556. || nfs_attribute_timeout(dir))
  557. return 0;
  558. return nfs_verify_change_attribute(dir, (unsigned long)dentry->d_fsdata);
  559. }
  560. static inline void nfs_set_verifier(struct dentry * dentry, unsigned long verf)
  561. {
  562. dentry->d_fsdata = (void *)verf;
  563. }
  564. /*
  565. * Whenever an NFS operation succeeds, we know that the dentry
  566. * is valid, so we update the revalidation timestamp.
  567. */
  568. static inline void nfs_renew_times(struct dentry * dentry)
  569. {
  570. dentry->d_time = jiffies;
  571. }
  572. /*
  573. * Return the intent data that applies to this particular path component
  574. *
  575. * Note that the current set of intents only apply to the very last
  576. * component of the path.
  577. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  578. */
  579. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  580. {
  581. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  582. return 0;
  583. return nd->flags & mask;
  584. }
  585. /*
  586. * Inode and filehandle revalidation for lookups.
  587. *
  588. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  589. * or if the intent information indicates that we're about to open this
  590. * particular file and the "nocto" mount flag is not set.
  591. *
  592. */
  593. static inline
  594. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  595. {
  596. struct nfs_server *server = NFS_SERVER(inode);
  597. if (nd != NULL) {
  598. /* VFS wants an on-the-wire revalidation */
  599. if (nd->flags & LOOKUP_REVAL)
  600. goto out_force;
  601. /* This is an open(2) */
  602. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  603. !(server->flags & NFS_MOUNT_NOCTO))
  604. goto out_force;
  605. }
  606. return nfs_revalidate_inode(server, inode);
  607. out_force:
  608. return __nfs_revalidate_inode(server, inode);
  609. }
  610. /*
  611. * We judge how long we want to trust negative
  612. * dentries by looking at the parent inode mtime.
  613. *
  614. * If parent mtime has changed, we revalidate, else we wait for a
  615. * period corresponding to the parent's attribute cache timeout value.
  616. */
  617. static inline
  618. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  619. struct nameidata *nd)
  620. {
  621. /* Don't revalidate a negative dentry if we're creating a new file */
  622. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  623. return 0;
  624. return !nfs_check_verifier(dir, dentry);
  625. }
  626. /*
  627. * This is called every time the dcache has a lookup hit,
  628. * and we should check whether we can really trust that
  629. * lookup.
  630. *
  631. * NOTE! The hit can be a negative hit too, don't assume
  632. * we have an inode!
  633. *
  634. * If the parent directory is seen to have changed, we throw out the
  635. * cached dentry and do a new lookup.
  636. */
  637. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  638. {
  639. struct inode *dir;
  640. struct inode *inode;
  641. struct dentry *parent;
  642. int error;
  643. struct nfs_fh fhandle;
  644. struct nfs_fattr fattr;
  645. unsigned long verifier;
  646. parent = dget_parent(dentry);
  647. lock_kernel();
  648. dir = parent->d_inode;
  649. inode = dentry->d_inode;
  650. if (!inode) {
  651. if (nfs_neg_need_reval(dir, dentry, nd))
  652. goto out_bad;
  653. goto out_valid;
  654. }
  655. if (is_bad_inode(inode)) {
  656. dfprintk(VFS, "nfs_lookup_validate: %s/%s has dud inode\n",
  657. dentry->d_parent->d_name.name, dentry->d_name.name);
  658. goto out_bad;
  659. }
  660. /* Revalidate parent directory attribute cache */
  661. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  662. goto out_zap_parent;
  663. /* Force a full look up iff the parent directory has changed */
  664. if (nfs_check_verifier(dir, dentry)) {
  665. if (nfs_lookup_verify_inode(inode, nd))
  666. goto out_zap_parent;
  667. goto out_valid;
  668. }
  669. if (NFS_STALE(inode))
  670. goto out_bad;
  671. verifier = nfs_save_change_attribute(dir);
  672. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  673. if (error)
  674. goto out_bad;
  675. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  676. goto out_bad;
  677. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  678. goto out_bad;
  679. nfs_renew_times(dentry);
  680. nfs_set_verifier(dentry, verifier);
  681. out_valid:
  682. unlock_kernel();
  683. dput(parent);
  684. return 1;
  685. out_zap_parent:
  686. nfs_zap_caches(dir);
  687. out_bad:
  688. NFS_CACHEINV(dir);
  689. if (inode && S_ISDIR(inode->i_mode)) {
  690. /* Purge readdir caches. */
  691. nfs_zap_caches(inode);
  692. /* If we have submounts, don't unhash ! */
  693. if (have_submounts(dentry))
  694. goto out_valid;
  695. shrink_dcache_parent(dentry);
  696. }
  697. d_drop(dentry);
  698. unlock_kernel();
  699. dput(parent);
  700. return 0;
  701. }
  702. /*
  703. * This is called from dput() when d_count is going to 0.
  704. */
  705. static int nfs_dentry_delete(struct dentry *dentry)
  706. {
  707. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  708. dentry->d_parent->d_name.name, dentry->d_name.name,
  709. dentry->d_flags);
  710. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  711. /* Unhash it, so that ->d_iput() would be called */
  712. return 1;
  713. }
  714. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  715. /* Unhash it, so that ancestors of killed async unlink
  716. * files will be cleaned up during umount */
  717. return 1;
  718. }
  719. return 0;
  720. }
  721. /*
  722. * Called when the dentry loses inode.
  723. * We use it to clean up silly-renamed files.
  724. */
  725. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  726. {
  727. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  728. lock_kernel();
  729. inode->i_nlink--;
  730. nfs_complete_unlink(dentry);
  731. unlock_kernel();
  732. }
  733. /* When creating a negative dentry, we want to renew d_time */
  734. nfs_renew_times(dentry);
  735. iput(inode);
  736. }
  737. struct dentry_operations nfs_dentry_operations = {
  738. .d_revalidate = nfs_lookup_revalidate,
  739. .d_delete = nfs_dentry_delete,
  740. .d_iput = nfs_dentry_iput,
  741. };
  742. /*
  743. * Use intent information to check whether or not we're going to do
  744. * an O_EXCL create using this path component.
  745. */
  746. static inline
  747. int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  748. {
  749. if (NFS_PROTO(dir)->version == 2)
  750. return 0;
  751. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_CREATE) == 0)
  752. return 0;
  753. return (nd->intent.open.flags & O_EXCL) != 0;
  754. }
  755. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  756. {
  757. struct dentry *res;
  758. struct inode *inode = NULL;
  759. int error;
  760. struct nfs_fh fhandle;
  761. struct nfs_fattr fattr;
  762. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  763. dentry->d_parent->d_name.name, dentry->d_name.name);
  764. res = ERR_PTR(-ENAMETOOLONG);
  765. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  766. goto out;
  767. res = ERR_PTR(-ENOMEM);
  768. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  769. lock_kernel();
  770. /* Revalidate parent directory attribute cache */
  771. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  772. if (error < 0) {
  773. res = ERR_PTR(error);
  774. goto out_unlock;
  775. }
  776. /* If we're doing an exclusive create, optimize away the lookup */
  777. if (nfs_is_exclusive_create(dir, nd))
  778. goto no_entry;
  779. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  780. if (error == -ENOENT)
  781. goto no_entry;
  782. if (error < 0) {
  783. res = ERR_PTR(error);
  784. goto out_unlock;
  785. }
  786. res = ERR_PTR(-EACCES);
  787. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  788. if (!inode)
  789. goto out_unlock;
  790. no_entry:
  791. res = d_add_unique(dentry, inode);
  792. if (res != NULL)
  793. dentry = res;
  794. nfs_renew_times(dentry);
  795. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  796. out_unlock:
  797. unlock_kernel();
  798. out:
  799. return res;
  800. }
  801. #ifdef CONFIG_NFS_V4
  802. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  803. struct dentry_operations nfs4_dentry_operations = {
  804. .d_revalidate = nfs_open_revalidate,
  805. .d_delete = nfs_dentry_delete,
  806. .d_iput = nfs_dentry_iput,
  807. };
  808. /*
  809. * Use intent information to determine whether we need to substitute
  810. * the NFSv4-style stateful OPEN for the LOOKUP call
  811. */
  812. static int is_atomic_open(struct inode *dir, struct nameidata *nd)
  813. {
  814. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  815. return 0;
  816. /* NFS does not (yet) have a stateful open for directories */
  817. if (nd->flags & LOOKUP_DIRECTORY)
  818. return 0;
  819. /* Are we trying to write to a read only partition? */
  820. if (IS_RDONLY(dir) && (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  821. return 0;
  822. return 1;
  823. }
  824. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  825. {
  826. struct dentry *res = NULL;
  827. struct inode *inode = NULL;
  828. int error;
  829. /* Check that we are indeed trying to open this file */
  830. if (!is_atomic_open(dir, nd))
  831. goto no_open;
  832. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  833. res = ERR_PTR(-ENAMETOOLONG);
  834. goto out;
  835. }
  836. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  837. /* Let vfs_create() deal with O_EXCL */
  838. if (nd->intent.open.flags & O_EXCL)
  839. goto no_entry;
  840. /* Open the file on the server */
  841. lock_kernel();
  842. /* Revalidate parent directory attribute cache */
  843. error = nfs_revalidate_inode(NFS_SERVER(dir), dir);
  844. if (error < 0) {
  845. res = ERR_PTR(error);
  846. goto out;
  847. }
  848. if (nd->intent.open.flags & O_CREAT) {
  849. nfs_begin_data_update(dir);
  850. inode = nfs4_atomic_open(dir, dentry, nd);
  851. nfs_end_data_update(dir);
  852. } else
  853. inode = nfs4_atomic_open(dir, dentry, nd);
  854. unlock_kernel();
  855. if (IS_ERR(inode)) {
  856. error = PTR_ERR(inode);
  857. switch (error) {
  858. /* Make a negative dentry */
  859. case -ENOENT:
  860. inode = NULL;
  861. break;
  862. /* This turned out not to be a regular file */
  863. case -ELOOP:
  864. if (!(nd->intent.open.flags & O_NOFOLLOW))
  865. goto no_open;
  866. /* case -EISDIR: */
  867. /* case -EINVAL: */
  868. default:
  869. res = ERR_PTR(error);
  870. goto out;
  871. }
  872. }
  873. no_entry:
  874. res = d_add_unique(dentry, inode);
  875. if (res != NULL)
  876. dentry = res;
  877. nfs_renew_times(dentry);
  878. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  879. out:
  880. return res;
  881. no_open:
  882. return nfs_lookup(dir, dentry, nd);
  883. }
  884. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  885. {
  886. struct dentry *parent = NULL;
  887. struct inode *inode = dentry->d_inode;
  888. struct inode *dir;
  889. unsigned long verifier;
  890. int openflags, ret = 0;
  891. parent = dget_parent(dentry);
  892. dir = parent->d_inode;
  893. if (!is_atomic_open(dir, nd))
  894. goto no_open;
  895. /* We can't create new files in nfs_open_revalidate(), so we
  896. * optimize away revalidation of negative dentries.
  897. */
  898. if (inode == NULL)
  899. goto out;
  900. /* NFS only supports OPEN on regular files */
  901. if (!S_ISREG(inode->i_mode))
  902. goto no_open;
  903. openflags = nd->intent.open.flags;
  904. /* We cannot do exclusive creation on a positive dentry */
  905. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  906. goto no_open;
  907. /* We can't create new files, or truncate existing ones here */
  908. openflags &= ~(O_CREAT|O_TRUNC);
  909. /*
  910. * Note: we're not holding inode->i_sem and so may be racing with
  911. * operations that change the directory. We therefore save the
  912. * change attribute *before* we do the RPC call.
  913. */
  914. lock_kernel();
  915. verifier = nfs_save_change_attribute(dir);
  916. ret = nfs4_open_revalidate(dir, dentry, openflags);
  917. if (!ret)
  918. nfs_set_verifier(dentry, verifier);
  919. unlock_kernel();
  920. out:
  921. dput(parent);
  922. if (!ret)
  923. d_drop(dentry);
  924. return ret;
  925. no_open:
  926. dput(parent);
  927. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  928. return 1;
  929. return nfs_lookup_revalidate(dentry, nd);
  930. }
  931. #endif /* CONFIG_NFSV4 */
  932. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  933. {
  934. struct dentry *parent = desc->file->f_dentry;
  935. struct inode *dir = parent->d_inode;
  936. struct nfs_entry *entry = desc->entry;
  937. struct dentry *dentry, *alias;
  938. struct qstr name = {
  939. .name = entry->name,
  940. .len = entry->len,
  941. };
  942. struct inode *inode;
  943. switch (name.len) {
  944. case 2:
  945. if (name.name[0] == '.' && name.name[1] == '.')
  946. return dget_parent(parent);
  947. break;
  948. case 1:
  949. if (name.name[0] == '.')
  950. return dget(parent);
  951. }
  952. name.hash = full_name_hash(name.name, name.len);
  953. dentry = d_lookup(parent, &name);
  954. if (dentry != NULL)
  955. return dentry;
  956. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  957. return NULL;
  958. /* Note: caller is already holding the dir->i_sem! */
  959. dentry = d_alloc(parent, &name);
  960. if (dentry == NULL)
  961. return NULL;
  962. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  963. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  964. if (!inode) {
  965. dput(dentry);
  966. return NULL;
  967. }
  968. alias = d_add_unique(dentry, inode);
  969. if (alias != NULL) {
  970. dput(dentry);
  971. dentry = alias;
  972. }
  973. nfs_renew_times(dentry);
  974. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  975. return dentry;
  976. }
  977. /*
  978. * Code common to create, mkdir, and mknod.
  979. */
  980. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  981. struct nfs_fattr *fattr)
  982. {
  983. struct inode *inode;
  984. int error = -EACCES;
  985. /* We may have been initialized further down */
  986. if (dentry->d_inode)
  987. return 0;
  988. if (fhandle->size == 0) {
  989. struct inode *dir = dentry->d_parent->d_inode;
  990. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  991. if (error)
  992. goto out_err;
  993. }
  994. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  995. struct nfs_server *server = NFS_SB(dentry->d_sb);
  996. error = server->rpc_ops->getattr(server, fhandle, fattr);
  997. if (error < 0)
  998. goto out_err;
  999. }
  1000. error = -ENOMEM;
  1001. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1002. if (inode == NULL)
  1003. goto out_err;
  1004. d_instantiate(dentry, inode);
  1005. return 0;
  1006. out_err:
  1007. d_drop(dentry);
  1008. return error;
  1009. }
  1010. /*
  1011. * Following a failed create operation, we drop the dentry rather
  1012. * than retain a negative dentry. This avoids a problem in the event
  1013. * that the operation succeeded on the server, but an error in the
  1014. * reply path made it appear to have failed.
  1015. */
  1016. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1017. struct nameidata *nd)
  1018. {
  1019. struct iattr attr;
  1020. int error;
  1021. int open_flags = 0;
  1022. dfprintk(VFS, "NFS: create(%s/%ld, %s\n", dir->i_sb->s_id,
  1023. dir->i_ino, dentry->d_name.name);
  1024. attr.ia_mode = mode;
  1025. attr.ia_valid = ATTR_MODE;
  1026. if (nd && (nd->flags & LOOKUP_CREATE))
  1027. open_flags = nd->intent.open.flags;
  1028. lock_kernel();
  1029. nfs_begin_data_update(dir);
  1030. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags);
  1031. nfs_end_data_update(dir);
  1032. if (error != 0)
  1033. goto out_err;
  1034. nfs_renew_times(dentry);
  1035. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1036. unlock_kernel();
  1037. return 0;
  1038. out_err:
  1039. unlock_kernel();
  1040. d_drop(dentry);
  1041. return error;
  1042. }
  1043. /*
  1044. * See comments for nfs_proc_create regarding failed operations.
  1045. */
  1046. static int
  1047. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1048. {
  1049. struct iattr attr;
  1050. int status;
  1051. dfprintk(VFS, "NFS: mknod(%s/%ld, %s\n", dir->i_sb->s_id,
  1052. dir->i_ino, dentry->d_name.name);
  1053. if (!new_valid_dev(rdev))
  1054. return -EINVAL;
  1055. attr.ia_mode = mode;
  1056. attr.ia_valid = ATTR_MODE;
  1057. lock_kernel();
  1058. nfs_begin_data_update(dir);
  1059. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1060. nfs_end_data_update(dir);
  1061. if (status != 0)
  1062. goto out_err;
  1063. nfs_renew_times(dentry);
  1064. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1065. unlock_kernel();
  1066. return 0;
  1067. out_err:
  1068. unlock_kernel();
  1069. d_drop(dentry);
  1070. return status;
  1071. }
  1072. /*
  1073. * See comments for nfs_proc_create regarding failed operations.
  1074. */
  1075. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1076. {
  1077. struct iattr attr;
  1078. int error;
  1079. dfprintk(VFS, "NFS: mkdir(%s/%ld, %s\n", dir->i_sb->s_id,
  1080. dir->i_ino, dentry->d_name.name);
  1081. attr.ia_valid = ATTR_MODE;
  1082. attr.ia_mode = mode | S_IFDIR;
  1083. lock_kernel();
  1084. nfs_begin_data_update(dir);
  1085. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1086. nfs_end_data_update(dir);
  1087. if (error != 0)
  1088. goto out_err;
  1089. nfs_renew_times(dentry);
  1090. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1091. unlock_kernel();
  1092. return 0;
  1093. out_err:
  1094. d_drop(dentry);
  1095. unlock_kernel();
  1096. return error;
  1097. }
  1098. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1099. {
  1100. int error;
  1101. dfprintk(VFS, "NFS: rmdir(%s/%ld, %s\n", dir->i_sb->s_id,
  1102. dir->i_ino, dentry->d_name.name);
  1103. lock_kernel();
  1104. nfs_begin_data_update(dir);
  1105. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1106. /* Ensure the VFS deletes this inode */
  1107. if (error == 0 && dentry->d_inode != NULL)
  1108. dentry->d_inode->i_nlink = 0;
  1109. nfs_end_data_update(dir);
  1110. unlock_kernel();
  1111. return error;
  1112. }
  1113. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1114. {
  1115. static unsigned int sillycounter;
  1116. const int i_inosize = sizeof(dir->i_ino)*2;
  1117. const int countersize = sizeof(sillycounter)*2;
  1118. const int slen = sizeof(".nfs") + i_inosize + countersize - 1;
  1119. char silly[slen+1];
  1120. struct qstr qsilly;
  1121. struct dentry *sdentry;
  1122. int error = -EIO;
  1123. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1124. dentry->d_parent->d_name.name, dentry->d_name.name,
  1125. atomic_read(&dentry->d_count));
  1126. #ifdef NFS_PARANOIA
  1127. if (!dentry->d_inode)
  1128. printk("NFS: silly-renaming %s/%s, negative dentry??\n",
  1129. dentry->d_parent->d_name.name, dentry->d_name.name);
  1130. #endif
  1131. /*
  1132. * We don't allow a dentry to be silly-renamed twice.
  1133. */
  1134. error = -EBUSY;
  1135. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1136. goto out;
  1137. sprintf(silly, ".nfs%*.*lx",
  1138. i_inosize, i_inosize, dentry->d_inode->i_ino);
  1139. sdentry = NULL;
  1140. do {
  1141. char *suffix = silly + slen - countersize;
  1142. dput(sdentry);
  1143. sillycounter++;
  1144. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1145. dfprintk(VFS, "trying to rename %s to %s\n",
  1146. dentry->d_name.name, silly);
  1147. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1148. /*
  1149. * N.B. Better to return EBUSY here ... it could be
  1150. * dangerous to delete the file while it's in use.
  1151. */
  1152. if (IS_ERR(sdentry))
  1153. goto out;
  1154. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1155. qsilly.name = silly;
  1156. qsilly.len = strlen(silly);
  1157. nfs_begin_data_update(dir);
  1158. if (dentry->d_inode) {
  1159. nfs_begin_data_update(dentry->d_inode);
  1160. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1161. dir, &qsilly);
  1162. nfs_end_data_update(dentry->d_inode);
  1163. } else
  1164. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1165. dir, &qsilly);
  1166. nfs_end_data_update(dir);
  1167. if (!error) {
  1168. nfs_renew_times(dentry);
  1169. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1170. d_move(dentry, sdentry);
  1171. error = nfs_async_unlink(dentry);
  1172. /* If we return 0 we don't unlink */
  1173. }
  1174. dput(sdentry);
  1175. out:
  1176. return error;
  1177. }
  1178. /*
  1179. * Remove a file after making sure there are no pending writes,
  1180. * and after checking that the file has only one user.
  1181. *
  1182. * We invalidate the attribute cache and free the inode prior to the operation
  1183. * to avoid possible races if the server reuses the inode.
  1184. */
  1185. static int nfs_safe_remove(struct dentry *dentry)
  1186. {
  1187. struct inode *dir = dentry->d_parent->d_inode;
  1188. struct inode *inode = dentry->d_inode;
  1189. int error = -EBUSY;
  1190. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1191. dentry->d_parent->d_name.name, dentry->d_name.name);
  1192. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1193. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1194. error = 0;
  1195. goto out;
  1196. }
  1197. nfs_begin_data_update(dir);
  1198. if (inode != NULL) {
  1199. nfs_begin_data_update(inode);
  1200. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1201. /* The VFS may want to delete this inode */
  1202. if (error == 0)
  1203. inode->i_nlink--;
  1204. nfs_end_data_update(inode);
  1205. } else
  1206. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1207. nfs_end_data_update(dir);
  1208. out:
  1209. return error;
  1210. }
  1211. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1212. * belongs to an active ".nfs..." file and we return -EBUSY.
  1213. *
  1214. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1215. */
  1216. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1217. {
  1218. int error;
  1219. int need_rehash = 0;
  1220. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1221. dir->i_ino, dentry->d_name.name);
  1222. lock_kernel();
  1223. spin_lock(&dcache_lock);
  1224. spin_lock(&dentry->d_lock);
  1225. if (atomic_read(&dentry->d_count) > 1) {
  1226. spin_unlock(&dentry->d_lock);
  1227. spin_unlock(&dcache_lock);
  1228. error = nfs_sillyrename(dir, dentry);
  1229. unlock_kernel();
  1230. return error;
  1231. }
  1232. if (!d_unhashed(dentry)) {
  1233. __d_drop(dentry);
  1234. need_rehash = 1;
  1235. }
  1236. spin_unlock(&dentry->d_lock);
  1237. spin_unlock(&dcache_lock);
  1238. error = nfs_safe_remove(dentry);
  1239. if (!error) {
  1240. nfs_renew_times(dentry);
  1241. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1242. } else if (need_rehash)
  1243. d_rehash(dentry);
  1244. unlock_kernel();
  1245. return error;
  1246. }
  1247. static int
  1248. nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1249. {
  1250. struct iattr attr;
  1251. struct nfs_fattr sym_attr;
  1252. struct nfs_fh sym_fh;
  1253. struct qstr qsymname;
  1254. int error;
  1255. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1256. dir->i_ino, dentry->d_name.name, symname);
  1257. #ifdef NFS_PARANOIA
  1258. if (dentry->d_inode)
  1259. printk("nfs_proc_symlink: %s/%s not negative!\n",
  1260. dentry->d_parent->d_name.name, dentry->d_name.name);
  1261. #endif
  1262. /*
  1263. * Fill in the sattr for the call.
  1264. * Note: SunOS 4.1.2 crashes if the mode isn't initialized!
  1265. */
  1266. attr.ia_valid = ATTR_MODE;
  1267. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1268. qsymname.name = symname;
  1269. qsymname.len = strlen(symname);
  1270. lock_kernel();
  1271. nfs_begin_data_update(dir);
  1272. error = NFS_PROTO(dir)->symlink(dir, &dentry->d_name, &qsymname,
  1273. &attr, &sym_fh, &sym_attr);
  1274. nfs_end_data_update(dir);
  1275. if (!error) {
  1276. error = nfs_instantiate(dentry, &sym_fh, &sym_attr);
  1277. } else {
  1278. if (error == -EEXIST)
  1279. printk("nfs_proc_symlink: %s/%s already exists??\n",
  1280. dentry->d_parent->d_name.name, dentry->d_name.name);
  1281. d_drop(dentry);
  1282. }
  1283. unlock_kernel();
  1284. return error;
  1285. }
  1286. static int
  1287. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1288. {
  1289. struct inode *inode = old_dentry->d_inode;
  1290. int error;
  1291. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1292. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1293. dentry->d_parent->d_name.name, dentry->d_name.name);
  1294. /*
  1295. * Drop the dentry in advance to force a new lookup.
  1296. * Since nfs_proc_link doesn't return a file handle,
  1297. * we can't use the existing dentry.
  1298. */
  1299. lock_kernel();
  1300. d_drop(dentry);
  1301. nfs_begin_data_update(dir);
  1302. nfs_begin_data_update(inode);
  1303. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1304. nfs_end_data_update(inode);
  1305. nfs_end_data_update(dir);
  1306. unlock_kernel();
  1307. return error;
  1308. }
  1309. /*
  1310. * RENAME
  1311. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1312. * different file handle for the same inode after a rename (e.g. when
  1313. * moving to a different directory). A fail-safe method to do so would
  1314. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1315. * rename the old file using the sillyrename stuff. This way, the original
  1316. * file in old_dir will go away when the last process iput()s the inode.
  1317. *
  1318. * FIXED.
  1319. *
  1320. * It actually works quite well. One needs to have the possibility for
  1321. * at least one ".nfs..." file in each directory the file ever gets
  1322. * moved or linked to which happens automagically with the new
  1323. * implementation that only depends on the dcache stuff instead of
  1324. * using the inode layer
  1325. *
  1326. * Unfortunately, things are a little more complicated than indicated
  1327. * above. For a cross-directory move, we want to make sure we can get
  1328. * rid of the old inode after the operation. This means there must be
  1329. * no pending writes (if it's a file), and the use count must be 1.
  1330. * If these conditions are met, we can drop the dentries before doing
  1331. * the rename.
  1332. */
  1333. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1334. struct inode *new_dir, struct dentry *new_dentry)
  1335. {
  1336. struct inode *old_inode = old_dentry->d_inode;
  1337. struct inode *new_inode = new_dentry->d_inode;
  1338. struct dentry *dentry = NULL, *rehash = NULL;
  1339. int error = -EBUSY;
  1340. /*
  1341. * To prevent any new references to the target during the rename,
  1342. * we unhash the dentry and free the inode in advance.
  1343. */
  1344. lock_kernel();
  1345. if (!d_unhashed(new_dentry)) {
  1346. d_drop(new_dentry);
  1347. rehash = new_dentry;
  1348. }
  1349. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1350. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1351. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1352. atomic_read(&new_dentry->d_count));
  1353. /*
  1354. * First check whether the target is busy ... we can't
  1355. * safely do _any_ rename if the target is in use.
  1356. *
  1357. * For files, make a copy of the dentry and then do a
  1358. * silly-rename. If the silly-rename succeeds, the
  1359. * copied dentry is hashed and becomes the new target.
  1360. */
  1361. if (!new_inode)
  1362. goto go_ahead;
  1363. if (S_ISDIR(new_inode->i_mode))
  1364. goto out;
  1365. else if (atomic_read(&new_dentry->d_count) > 2) {
  1366. int err;
  1367. /* copy the target dentry's name */
  1368. dentry = d_alloc(new_dentry->d_parent,
  1369. &new_dentry->d_name);
  1370. if (!dentry)
  1371. goto out;
  1372. /* silly-rename the existing target ... */
  1373. err = nfs_sillyrename(new_dir, new_dentry);
  1374. if (!err) {
  1375. new_dentry = rehash = dentry;
  1376. new_inode = NULL;
  1377. /* instantiate the replacement target */
  1378. d_instantiate(new_dentry, NULL);
  1379. } else if (atomic_read(&new_dentry->d_count) > 1) {
  1380. /* dentry still busy? */
  1381. #ifdef NFS_PARANOIA
  1382. printk("nfs_rename: target %s/%s busy, d_count=%d\n",
  1383. new_dentry->d_parent->d_name.name,
  1384. new_dentry->d_name.name,
  1385. atomic_read(&new_dentry->d_count));
  1386. #endif
  1387. goto out;
  1388. }
  1389. }
  1390. go_ahead:
  1391. /*
  1392. * ... prune child dentries and writebacks if needed.
  1393. */
  1394. if (atomic_read(&old_dentry->d_count) > 1) {
  1395. nfs_wb_all(old_inode);
  1396. shrink_dcache_parent(old_dentry);
  1397. }
  1398. if (new_inode)
  1399. d_delete(new_dentry);
  1400. nfs_begin_data_update(old_dir);
  1401. nfs_begin_data_update(new_dir);
  1402. nfs_begin_data_update(old_inode);
  1403. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1404. new_dir, &new_dentry->d_name);
  1405. nfs_end_data_update(old_inode);
  1406. nfs_end_data_update(new_dir);
  1407. nfs_end_data_update(old_dir);
  1408. out:
  1409. if (rehash)
  1410. d_rehash(rehash);
  1411. if (!error) {
  1412. if (!S_ISDIR(old_inode->i_mode))
  1413. d_move(old_dentry, new_dentry);
  1414. nfs_renew_times(new_dentry);
  1415. nfs_set_verifier(new_dentry, nfs_save_change_attribute(new_dir));
  1416. }
  1417. /* new dentry created? */
  1418. if (dentry)
  1419. dput(dentry);
  1420. unlock_kernel();
  1421. return error;
  1422. }
  1423. int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1424. {
  1425. struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
  1426. if (cache->cred != cred
  1427. || time_after(jiffies, cache->jiffies + NFS_ATTRTIMEO(inode))
  1428. || (NFS_FLAGS(inode) & NFS_INO_INVALID_ACCESS))
  1429. return -ENOENT;
  1430. memcpy(res, cache, sizeof(*res));
  1431. return 0;
  1432. }
  1433. void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1434. {
  1435. struct nfs_access_entry *cache = &NFS_I(inode)->cache_access;
  1436. if (cache->cred != set->cred) {
  1437. if (cache->cred)
  1438. put_rpccred(cache->cred);
  1439. cache->cred = get_rpccred(set->cred);
  1440. }
  1441. NFS_FLAGS(inode) &= ~NFS_INO_INVALID_ACCESS;
  1442. cache->jiffies = set->jiffies;
  1443. cache->mask = set->mask;
  1444. }
  1445. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1446. {
  1447. struct nfs_access_entry cache;
  1448. int status;
  1449. status = nfs_access_get_cached(inode, cred, &cache);
  1450. if (status == 0)
  1451. goto out;
  1452. /* Be clever: ask server to check for all possible rights */
  1453. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1454. cache.cred = cred;
  1455. cache.jiffies = jiffies;
  1456. status = NFS_PROTO(inode)->access(inode, &cache);
  1457. if (status != 0)
  1458. return status;
  1459. nfs_access_add_cache(inode, &cache);
  1460. out:
  1461. if ((cache.mask & mask) == mask)
  1462. return 0;
  1463. return -EACCES;
  1464. }
  1465. int nfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  1466. {
  1467. struct rpc_cred *cred;
  1468. int res = 0;
  1469. if (mask == 0)
  1470. goto out;
  1471. /* Is this sys_access() ? */
  1472. if (nd != NULL && (nd->flags & LOOKUP_ACCESS))
  1473. goto force_lookup;
  1474. switch (inode->i_mode & S_IFMT) {
  1475. case S_IFLNK:
  1476. goto out;
  1477. case S_IFREG:
  1478. /* NFSv4 has atomic_open... */
  1479. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1480. && nd != NULL
  1481. && (nd->flags & LOOKUP_OPEN))
  1482. goto out;
  1483. break;
  1484. case S_IFDIR:
  1485. /*
  1486. * Optimize away all write operations, since the server
  1487. * will check permissions when we perform the op.
  1488. */
  1489. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1490. goto out;
  1491. }
  1492. force_lookup:
  1493. lock_kernel();
  1494. if (!NFS_PROTO(inode)->access)
  1495. goto out_notsup;
  1496. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1497. if (!IS_ERR(cred)) {
  1498. res = nfs_do_access(inode, cred, mask);
  1499. put_rpccred(cred);
  1500. } else
  1501. res = PTR_ERR(cred);
  1502. unlock_kernel();
  1503. out:
  1504. return res;
  1505. out_notsup:
  1506. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1507. if (res == 0)
  1508. res = generic_permission(inode, mask, NULL);
  1509. unlock_kernel();
  1510. return res;
  1511. }
  1512. /*
  1513. * Local variables:
  1514. * version-control: t
  1515. * kept-new-versions: 5
  1516. * End:
  1517. */