dir.c 41 KB

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