expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #include <linux/fs.h>
  2. #include <linux/file.h>
  3. #include <linux/module.h>
  4. #include <linux/namei.h>
  5. struct export_operations export_op_default;
  6. #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
  7. #define dprintk(fmt, args...) do{}while(0)
  8. static struct dentry *
  9. find_acceptable_alias(struct dentry *result,
  10. int (*acceptable)(void *context, struct dentry *dentry),
  11. void *context)
  12. {
  13. struct dentry *dentry, *toput = NULL;
  14. spin_lock(&dcache_lock);
  15. list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
  16. dget_locked(dentry);
  17. spin_unlock(&dcache_lock);
  18. if (toput)
  19. dput(toput);
  20. if (dentry != result && acceptable(context, dentry)) {
  21. dput(result);
  22. return dentry;
  23. }
  24. spin_lock(&dcache_lock);
  25. toput = dentry;
  26. }
  27. spin_unlock(&dcache_lock);
  28. if (toput)
  29. dput(toput);
  30. return NULL;
  31. }
  32. /**
  33. * find_exported_dentry - helper routine to implement export_operations->decode_fh
  34. * @sb: The &super_block identifying the filesystem
  35. * @obj: An opaque identifier of the object to be found - passed to
  36. * get_inode
  37. * @parent: An optional opqaue identifier of the parent of the object.
  38. * @acceptable: A function used to test possible &dentries to see if they are
  39. * acceptable
  40. * @context: A parameter to @acceptable so that it knows on what basis to
  41. * judge.
  42. *
  43. * find_exported_dentry is the central helper routine to enable file systems
  44. * to provide the decode_fh() export_operation. It's main task is to take
  45. * an &inode, find or create an appropriate &dentry structure, and possibly
  46. * splice this into the dcache in the correct place.
  47. *
  48. * The decode_fh() operation provided by the filesystem should call
  49. * find_exported_dentry() with the same parameters that it received except
  50. * that instead of the file handle fragment, pointers to opaque identifiers
  51. * for the object and optionally its parent are passed. The default decode_fh
  52. * routine passes one pointer to the start of the filehandle fragment, and
  53. * one 8 bytes into the fragment. It is expected that most filesystems will
  54. * take this approach, though the offset to the parent identifier may well be
  55. * different.
  56. *
  57. * find_exported_dentry() will call get_dentry to get an dentry pointer from
  58. * the file system. If any &dentry in the d_alias list is acceptable, it will
  59. * be returned. Otherwise find_exported_dentry() will attempt to splice a new
  60. * &dentry into the dcache using get_name() and get_parent() to find the
  61. * appropriate place.
  62. */
  63. struct dentry *
  64. find_exported_dentry(struct super_block *sb, void *obj, void *parent,
  65. int (*acceptable)(void *context, struct dentry *de),
  66. void *context)
  67. {
  68. struct dentry *result = NULL;
  69. struct dentry *target_dir;
  70. int err;
  71. struct export_operations *nops = sb->s_export_op;
  72. struct dentry *alias;
  73. int noprogress;
  74. char nbuf[NAME_MAX+1];
  75. /*
  76. * Attempt to find the inode.
  77. */
  78. result = CALL(sb->s_export_op,get_dentry)(sb,obj);
  79. err = -ESTALE;
  80. if (result == NULL)
  81. goto err_out;
  82. if (IS_ERR(result)) {
  83. err = PTR_ERR(result);
  84. goto err_out;
  85. }
  86. if (S_ISDIR(result->d_inode->i_mode) &&
  87. (result->d_flags & DCACHE_DISCONNECTED)) {
  88. /* it is an unconnected directory, we must connect it */
  89. ;
  90. } else {
  91. if (acceptable(context, result))
  92. return result;
  93. if (S_ISDIR(result->d_inode->i_mode)) {
  94. err = -EACCES;
  95. goto err_result;
  96. }
  97. alias = find_acceptable_alias(result, acceptable, context);
  98. if (alias)
  99. return alias;
  100. }
  101. /* It's a directory, or we are required to confirm the file's
  102. * location in the tree based on the parent information
  103. */
  104. dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
  105. if (S_ISDIR(result->d_inode->i_mode))
  106. target_dir = dget(result);
  107. else {
  108. if (parent == NULL)
  109. goto err_result;
  110. target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
  111. if (IS_ERR(target_dir))
  112. err = PTR_ERR(target_dir);
  113. if (target_dir == NULL || IS_ERR(target_dir))
  114. goto err_result;
  115. }
  116. /*
  117. * Now we need to make sure that target_dir is properly connected.
  118. * It may already be, as the flag isn't always updated when connection
  119. * happens.
  120. * So, we walk up parent links until we find a connected directory,
  121. * or we run out of directories. Then we find the parent, find
  122. * the name of the child in that parent, and do a lookup.
  123. * This should connect the child into the parent
  124. * We then repeat.
  125. */
  126. /* it is possible that a confused file system might not let us complete
  127. * the path to the root. For example, if get_parent returns a directory
  128. * in which we cannot find a name for the child. While this implies a
  129. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  130. * the noprogress counter. If we go through the loop 10 times (2 is
  131. * probably enough) without getting anywhere, we just give up
  132. */
  133. noprogress= 0;
  134. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  135. struct dentry *pd = target_dir;
  136. dget(pd);
  137. spin_lock(&pd->d_lock);
  138. while (!IS_ROOT(pd) &&
  139. (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
  140. struct dentry *parent = pd->d_parent;
  141. dget(parent);
  142. spin_unlock(&pd->d_lock);
  143. dput(pd);
  144. pd = parent;
  145. spin_lock(&pd->d_lock);
  146. }
  147. spin_unlock(&pd->d_lock);
  148. if (!IS_ROOT(pd)) {
  149. /* must have found a connected parent - great */
  150. spin_lock(&pd->d_lock);
  151. pd->d_flags &= ~DCACHE_DISCONNECTED;
  152. spin_unlock(&pd->d_lock);
  153. noprogress = 0;
  154. } else if (pd == sb->s_root) {
  155. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  156. spin_lock(&pd->d_lock);
  157. pd->d_flags &= ~DCACHE_DISCONNECTED;
  158. spin_unlock(&pd->d_lock);
  159. noprogress = 0;
  160. } else {
  161. /* we have hit the top of a disconnected path. Try
  162. * to find parent and connect
  163. * note: racing with some other process renaming a
  164. * directory isn't much of a problem here. If someone
  165. * renames the directory, it will end up properly
  166. * connected, which is what we want
  167. */
  168. struct dentry *ppd;
  169. struct dentry *npd;
  170. mutex_lock(&pd->d_inode->i_mutex);
  171. ppd = CALL(nops,get_parent)(pd);
  172. mutex_unlock(&pd->d_inode->i_mutex);
  173. if (IS_ERR(ppd)) {
  174. err = PTR_ERR(ppd);
  175. dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
  176. pd->d_inode->i_ino, err);
  177. dput(pd);
  178. break;
  179. }
  180. dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
  181. err = CALL(nops,get_name)(ppd, nbuf, pd);
  182. if (err) {
  183. dput(ppd);
  184. dput(pd);
  185. if (err == -ENOENT)
  186. /* some race between get_parent and
  187. * get_name? just try again
  188. */
  189. continue;
  190. break;
  191. }
  192. dprintk("find_exported_dentry: found name: %s\n", nbuf);
  193. mutex_lock(&ppd->d_inode->i_mutex);
  194. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  195. mutex_unlock(&ppd->d_inode->i_mutex);
  196. if (IS_ERR(npd)) {
  197. err = PTR_ERR(npd);
  198. dprintk("find_exported_dentry: lookup failed: %d\n", err);
  199. dput(ppd);
  200. dput(pd);
  201. break;
  202. }
  203. /* we didn't really want npd, we really wanted
  204. * a side-effect of the lookup.
  205. * hopefully, npd == pd, though it isn't really
  206. * a problem if it isn't
  207. */
  208. if (npd == pd)
  209. noprogress = 0;
  210. else
  211. printk("find_exported_dentry: npd != pd\n");
  212. dput(npd);
  213. dput(ppd);
  214. if (IS_ROOT(pd)) {
  215. /* something went wrong, we have to give up */
  216. dput(pd);
  217. break;
  218. }
  219. }
  220. dput(pd);
  221. }
  222. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  223. /* something went wrong - oh-well */
  224. if (!err)
  225. err = -ESTALE;
  226. goto err_target;
  227. }
  228. /* if we weren't after a directory, have one more step to go */
  229. if (result != target_dir) {
  230. struct dentry *nresult;
  231. err = CALL(nops,get_name)(target_dir, nbuf, result);
  232. if (!err) {
  233. mutex_lock(&target_dir->d_inode->i_mutex);
  234. nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
  235. mutex_unlock(&target_dir->d_inode->i_mutex);
  236. if (!IS_ERR(nresult)) {
  237. if (nresult->d_inode) {
  238. dput(result);
  239. result = nresult;
  240. } else
  241. dput(nresult);
  242. }
  243. }
  244. }
  245. dput(target_dir);
  246. /* now result is properly connected, it is our best bet */
  247. if (acceptable(context, result))
  248. return result;
  249. alias = find_acceptable_alias(result, acceptable, context);
  250. if (alias)
  251. return alias;
  252. /* drat - I just cannot find anything acceptable */
  253. dput(result);
  254. /* It might be justifiable to return ESTALE here,
  255. * but the filehandle at-least looks reasonable good
  256. * and it just be a permission problem, so returning
  257. * -EACCESS is safer
  258. */
  259. return ERR_PTR(-EACCES);
  260. err_target:
  261. dput(target_dir);
  262. err_result:
  263. dput(result);
  264. err_out:
  265. return ERR_PTR(err);
  266. }
  267. static struct dentry *get_parent(struct dentry *child)
  268. {
  269. /* get_parent cannot be supported generically, the locking
  270. * is too icky.
  271. * instead, we just return EACCES. If server reboots or inodes
  272. * get flushed, you lose
  273. */
  274. return ERR_PTR(-EACCES);
  275. }
  276. struct getdents_callback {
  277. char *name; /* name that was found. It already points to a
  278. buffer NAME_MAX+1 is size */
  279. unsigned long ino; /* the inum we are looking for */
  280. int found; /* inode matched? */
  281. int sequence; /* sequence counter */
  282. };
  283. /*
  284. * A rather strange filldir function to capture
  285. * the name matching the specified inode number.
  286. */
  287. static int filldir_one(void * __buf, const char * name, int len,
  288. loff_t pos, u64 ino, unsigned int d_type)
  289. {
  290. struct getdents_callback *buf = __buf;
  291. int result = 0;
  292. buf->sequence++;
  293. if (buf->ino == ino) {
  294. memcpy(buf->name, name, len);
  295. buf->name[len] = '\0';
  296. buf->found = 1;
  297. result = -1;
  298. }
  299. return result;
  300. }
  301. /**
  302. * get_name - default export_operations->get_name function
  303. * @dentry: the directory in which to find a name
  304. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  305. * @child: the dentry for the child directory.
  306. *
  307. * calls readdir on the parent until it finds an entry with
  308. * the same inode number as the child, and returns that.
  309. */
  310. static int get_name(struct dentry *dentry, char *name,
  311. struct dentry *child)
  312. {
  313. struct inode *dir = dentry->d_inode;
  314. int error;
  315. struct file *file;
  316. struct getdents_callback buffer;
  317. error = -ENOTDIR;
  318. if (!dir || !S_ISDIR(dir->i_mode))
  319. goto out;
  320. error = -EINVAL;
  321. if (!dir->i_fop)
  322. goto out;
  323. /*
  324. * Open the directory ...
  325. */
  326. file = dentry_open(dget(dentry), NULL, O_RDONLY);
  327. error = PTR_ERR(file);
  328. if (IS_ERR(file))
  329. goto out;
  330. error = -EINVAL;
  331. if (!file->f_op->readdir)
  332. goto out_close;
  333. buffer.name = name;
  334. buffer.ino = child->d_inode->i_ino;
  335. buffer.found = 0;
  336. buffer.sequence = 0;
  337. while (1) {
  338. int old_seq = buffer.sequence;
  339. error = vfs_readdir(file, filldir_one, &buffer);
  340. if (error < 0)
  341. break;
  342. error = 0;
  343. if (buffer.found)
  344. break;
  345. error = -ENOENT;
  346. if (old_seq == buffer.sequence)
  347. break;
  348. }
  349. out_close:
  350. fput(file);
  351. out:
  352. return error;
  353. }
  354. static struct dentry *export_iget(struct super_block *sb, unsigned long ino, __u32 generation)
  355. {
  356. /* iget isn't really right if the inode is currently unallocated!!
  357. * This should really all be done inside each filesystem
  358. *
  359. * ext2fs' read_inode has been strengthed to return a bad_inode if
  360. * the inode had been deleted.
  361. *
  362. * Currently we don't know the generation for parent directory, so
  363. * a generation of 0 means "accept any"
  364. */
  365. struct inode *inode;
  366. struct dentry *result;
  367. if (ino == 0)
  368. return ERR_PTR(-ESTALE);
  369. inode = iget(sb, ino);
  370. if (inode == NULL)
  371. return ERR_PTR(-ENOMEM);
  372. if (is_bad_inode(inode)
  373. || (generation && inode->i_generation != generation)
  374. ) {
  375. /* we didn't find the right inode.. */
  376. dprintk("fh_verify: Inode %lu, Bad count: %d %d or version %u %u\n",
  377. inode->i_ino,
  378. inode->i_nlink, atomic_read(&inode->i_count),
  379. inode->i_generation,
  380. generation);
  381. iput(inode);
  382. return ERR_PTR(-ESTALE);
  383. }
  384. /* now to find a dentry.
  385. * If possible, get a well-connected one
  386. */
  387. result = d_alloc_anon(inode);
  388. if (!result) {
  389. iput(inode);
  390. return ERR_PTR(-ENOMEM);
  391. }
  392. return result;
  393. }
  394. static struct dentry *get_object(struct super_block *sb, void *vobjp)
  395. {
  396. __u32 *objp = vobjp;
  397. unsigned long ino = objp[0];
  398. __u32 generation = objp[1];
  399. return export_iget(sb, ino, generation);
  400. }
  401. /**
  402. * export_encode_fh - default export_operations->encode_fh function
  403. * @dentry: the dentry to encode
  404. * @fh: where to store the file handle fragment
  405. * @max_len: maximum length to store there
  406. * @connectable: whether to store parent information
  407. *
  408. * This default encode_fh function assumes that the 32 inode number
  409. * is suitable for locating an inode, and that the generation number
  410. * can be used to check that it is still valid. It places them in the
  411. * filehandle fragment where export_decode_fh expects to find them.
  412. */
  413. static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  414. int connectable)
  415. {
  416. struct inode * inode = dentry->d_inode;
  417. int len = *max_len;
  418. int type = 1;
  419. if (len < 2 || (connectable && len < 4))
  420. return 255;
  421. len = 2;
  422. fh[0] = inode->i_ino;
  423. fh[1] = inode->i_generation;
  424. if (connectable && !S_ISDIR(inode->i_mode)) {
  425. struct inode *parent;
  426. spin_lock(&dentry->d_lock);
  427. parent = dentry->d_parent->d_inode;
  428. fh[2] = parent->i_ino;
  429. fh[3] = parent->i_generation;
  430. spin_unlock(&dentry->d_lock);
  431. len = 4;
  432. type = 2;
  433. }
  434. *max_len = len;
  435. return type;
  436. }
  437. /**
  438. * export_decode_fh - default export_operations->decode_fh function
  439. * @sb: The superblock
  440. * @fh: pointer to the file handle fragment
  441. * @fh_len: length of file handle fragment
  442. * @acceptable: function for testing acceptability of dentrys
  443. * @context: context for @acceptable
  444. *
  445. * This is the default decode_fh() function.
  446. * a fileid_type of 1 indicates that the filehandlefragment
  447. * just contains an object identifier understood by get_dentry.
  448. * a fileid_type of 2 says that there is also a directory
  449. * identifier 8 bytes in to the filehandlefragement.
  450. */
  451. static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
  452. int fileid_type,
  453. int (*acceptable)(void *context, struct dentry *de),
  454. void *context)
  455. {
  456. __u32 parent[2];
  457. parent[0] = parent[1] = 0;
  458. if (fh_len < 2 || fileid_type > 2)
  459. return NULL;
  460. if (fileid_type == 2) {
  461. if (fh_len > 2) parent[0] = fh[2];
  462. if (fh_len > 3) parent[1] = fh[3];
  463. }
  464. return find_exported_dentry(sb, fh, parent,
  465. acceptable, context);
  466. }
  467. struct export_operations export_op_default = {
  468. .decode_fh = export_decode_fh,
  469. .encode_fh = export_encode_fh,
  470. .get_name = get_name,
  471. .get_parent = get_parent,
  472. .get_dentry = get_object,
  473. };
  474. EXPORT_SYMBOL(export_op_default);
  475. EXPORT_SYMBOL(find_exported_dentry);
  476. MODULE_LICENSE("GPL");