super.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. #include "protocol.h"
  7. #include "orangefs-kernel.h"
  8. #include "orangefs-bufmap.h"
  9. #include <linux/parser.h>
  10. /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
  11. static struct kmem_cache *orangefs_inode_cache;
  12. /* list for storing orangefs specific superblocks in use */
  13. LIST_HEAD(orangefs_superblocks);
  14. DEFINE_SPINLOCK(orangefs_superblocks_lock);
  15. enum {
  16. Opt_intr,
  17. Opt_acl,
  18. Opt_local_lock,
  19. Opt_err
  20. };
  21. static const match_table_t tokens = {
  22. { Opt_acl, "acl" },
  23. { Opt_intr, "intr" },
  24. { Opt_local_lock, "local_lock" },
  25. { Opt_err, NULL }
  26. };
  27. uint64_t orangefs_features;
  28. static int parse_mount_options(struct super_block *sb, char *options,
  29. int silent)
  30. {
  31. struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
  32. substring_t args[MAX_OPT_ARGS];
  33. char *p;
  34. /*
  35. * Force any potential flags that might be set from the mount
  36. * to zero, ie, initialize to unset.
  37. */
  38. sb->s_flags &= ~MS_POSIXACL;
  39. orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
  40. orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
  41. while ((p = strsep(&options, ",")) != NULL) {
  42. int token;
  43. if (!*p)
  44. continue;
  45. token = match_token(p, tokens, args);
  46. switch (token) {
  47. case Opt_acl:
  48. sb->s_flags |= MS_POSIXACL;
  49. break;
  50. case Opt_intr:
  51. orangefs_sb->flags |= ORANGEFS_OPT_INTR;
  52. break;
  53. case Opt_local_lock:
  54. orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
  55. break;
  56. default:
  57. goto fail;
  58. }
  59. }
  60. return 0;
  61. fail:
  62. if (!silent)
  63. gossip_err("Error: mount option [%s] is not supported.\n", p);
  64. return -EINVAL;
  65. }
  66. static void orangefs_inode_cache_ctor(void *req)
  67. {
  68. struct orangefs_inode_s *orangefs_inode = req;
  69. inode_init_once(&orangefs_inode->vfs_inode);
  70. init_rwsem(&orangefs_inode->xattr_sem);
  71. orangefs_inode->vfs_inode.i_version = 1;
  72. }
  73. static struct inode *orangefs_alloc_inode(struct super_block *sb)
  74. {
  75. struct orangefs_inode_s *orangefs_inode;
  76. orangefs_inode = kmem_cache_alloc(orangefs_inode_cache, GFP_KERNEL);
  77. if (orangefs_inode == NULL) {
  78. gossip_err("Failed to allocate orangefs_inode\n");
  79. return NULL;
  80. }
  81. /*
  82. * We want to clear everything except for rw_semaphore and the
  83. * vfs_inode.
  84. */
  85. memset(&orangefs_inode->refn.khandle, 0, 16);
  86. orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
  87. orangefs_inode->last_failed_block_index_read = 0;
  88. memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
  89. orangefs_inode->pinode_flags = 0;
  90. gossip_debug(GOSSIP_SUPER_DEBUG,
  91. "orangefs_alloc_inode: allocated %p\n",
  92. &orangefs_inode->vfs_inode);
  93. return &orangefs_inode->vfs_inode;
  94. }
  95. static void orangefs_i_callback(struct rcu_head *head)
  96. {
  97. struct inode *inode = container_of(head, struct inode, i_rcu);
  98. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  99. kmem_cache_free(orangefs_inode_cache, orangefs_inode);
  100. }
  101. static void orangefs_destroy_inode(struct inode *inode)
  102. {
  103. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  104. gossip_debug(GOSSIP_SUPER_DEBUG,
  105. "%s: deallocated %p destroying inode %pU\n",
  106. __func__, orangefs_inode, get_khandle_from_ino(inode));
  107. call_rcu(&inode->i_rcu, orangefs_i_callback);
  108. }
  109. /*
  110. * NOTE: information filled in here is typically reflected in the
  111. * output of the system command 'df'
  112. */
  113. static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
  114. {
  115. int ret = -ENOMEM;
  116. struct orangefs_kernel_op_s *new_op = NULL;
  117. int flags = 0;
  118. struct super_block *sb = NULL;
  119. sb = dentry->d_sb;
  120. gossip_debug(GOSSIP_SUPER_DEBUG,
  121. "orangefs_statfs: called on sb %p (fs_id is %d)\n",
  122. sb,
  123. (int)(ORANGEFS_SB(sb)->fs_id));
  124. new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
  125. if (!new_op)
  126. return ret;
  127. new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
  128. if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
  129. flags = ORANGEFS_OP_INTERRUPTIBLE;
  130. ret = service_operation(new_op, "orangefs_statfs", flags);
  131. if (new_op->downcall.status < 0)
  132. goto out_op_release;
  133. gossip_debug(GOSSIP_SUPER_DEBUG,
  134. "%s: got %ld blocks available | "
  135. "%ld blocks total | %ld block size | "
  136. "%ld files total | %ld files avail\n",
  137. __func__,
  138. (long)new_op->downcall.resp.statfs.blocks_avail,
  139. (long)new_op->downcall.resp.statfs.blocks_total,
  140. (long)new_op->downcall.resp.statfs.block_size,
  141. (long)new_op->downcall.resp.statfs.files_total,
  142. (long)new_op->downcall.resp.statfs.files_avail);
  143. buf->f_type = sb->s_magic;
  144. memcpy(&buf->f_fsid, &ORANGEFS_SB(sb)->fs_id, sizeof(buf->f_fsid));
  145. buf->f_bsize = new_op->downcall.resp.statfs.block_size;
  146. buf->f_namelen = ORANGEFS_NAME_MAX;
  147. buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
  148. buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  149. buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  150. buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
  151. buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
  152. buf->f_frsize = sb->s_blocksize;
  153. out_op_release:
  154. op_release(new_op);
  155. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_statfs: returning %d\n", ret);
  156. return ret;
  157. }
  158. /*
  159. * Remount as initiated by VFS layer. We just need to reparse the mount
  160. * options, no need to signal pvfs2-client-core about it.
  161. */
  162. static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
  163. {
  164. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
  165. return parse_mount_options(sb, data, 1);
  166. }
  167. /*
  168. * Remount as initiated by pvfs2-client-core on restart. This is used to
  169. * repopulate mount information left from previous pvfs2-client-core.
  170. *
  171. * the idea here is that given a valid superblock, we're
  172. * re-initializing the user space client with the initial mount
  173. * information specified when the super block was first initialized.
  174. * this is very different than the first initialization/creation of a
  175. * superblock. we use the special service_priority_operation to make
  176. * sure that the mount gets ahead of any other pending operation that
  177. * is waiting for servicing. this means that the pvfs2-client won't
  178. * fail to start several times for all other pending operations before
  179. * the client regains all of the mount information from us.
  180. * NOTE: this function assumes that the request_mutex is already acquired!
  181. */
  182. int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
  183. {
  184. struct orangefs_kernel_op_s *new_op;
  185. int ret = -EINVAL;
  186. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
  187. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  188. if (!new_op)
  189. return -ENOMEM;
  190. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  191. orangefs_sb->devname,
  192. ORANGEFS_MAX_SERVER_ADDR_LEN);
  193. gossip_debug(GOSSIP_SUPER_DEBUG,
  194. "Attempting ORANGEFS Remount via host %s\n",
  195. new_op->upcall.req.fs_mount.orangefs_config_server);
  196. /*
  197. * we assume that the calling function has already acquired the
  198. * request_mutex to prevent other operations from bypassing
  199. * this one
  200. */
  201. ret = service_operation(new_op, "orangefs_remount",
  202. ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
  203. gossip_debug(GOSSIP_SUPER_DEBUG,
  204. "orangefs_remount: mount got return value of %d\n",
  205. ret);
  206. if (ret == 0) {
  207. /*
  208. * store the id assigned to this sb -- it's just a
  209. * short-lived mapping that the system interface uses
  210. * to map this superblock to a particular mount entry
  211. */
  212. orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
  213. orangefs_sb->mount_pending = 0;
  214. }
  215. op_release(new_op);
  216. if (orangefs_userspace_version >= 20906) {
  217. new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
  218. if (!new_op)
  219. return -ENOMEM;
  220. new_op->upcall.req.features.features = 0;
  221. ret = service_operation(new_op, "orangefs_features",
  222. ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
  223. if (!ret)
  224. orangefs_features =
  225. new_op->downcall.resp.features.features;
  226. else
  227. orangefs_features = 0;
  228. op_release(new_op);
  229. } else {
  230. orangefs_features = 0;
  231. }
  232. return ret;
  233. }
  234. int fsid_key_table_initialize(void)
  235. {
  236. return 0;
  237. }
  238. void fsid_key_table_finalize(void)
  239. {
  240. }
  241. /* Called whenever the VFS dirties the inode in response to atime updates */
  242. static void orangefs_dirty_inode(struct inode *inode, int flags)
  243. {
  244. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  245. gossip_debug(GOSSIP_SUPER_DEBUG,
  246. "orangefs_dirty_inode: %pU\n",
  247. get_khandle_from_ino(inode));
  248. SetAtimeFlag(orangefs_inode);
  249. }
  250. static const struct super_operations orangefs_s_ops = {
  251. .alloc_inode = orangefs_alloc_inode,
  252. .destroy_inode = orangefs_destroy_inode,
  253. .dirty_inode = orangefs_dirty_inode,
  254. .drop_inode = generic_delete_inode,
  255. .statfs = orangefs_statfs,
  256. .remount_fs = orangefs_remount_fs,
  257. .show_options = generic_show_options,
  258. };
  259. static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
  260. struct fid *fid,
  261. int fh_len,
  262. int fh_type)
  263. {
  264. struct orangefs_object_kref refn;
  265. if (fh_len < 5 || fh_type > 2)
  266. return NULL;
  267. ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
  268. refn.fs_id = (u32) fid->raw[4];
  269. gossip_debug(GOSSIP_SUPER_DEBUG,
  270. "fh_to_dentry: handle %pU, fs_id %d\n",
  271. &refn.khandle,
  272. refn.fs_id);
  273. return d_obtain_alias(orangefs_iget(sb, &refn));
  274. }
  275. static int orangefs_encode_fh(struct inode *inode,
  276. __u32 *fh,
  277. int *max_len,
  278. struct inode *parent)
  279. {
  280. int len = parent ? 10 : 5;
  281. int type = 1;
  282. struct orangefs_object_kref refn;
  283. if (*max_len < len) {
  284. gossip_lerr("fh buffer is too small for encoding\n");
  285. *max_len = len;
  286. type = 255;
  287. goto out;
  288. }
  289. refn = ORANGEFS_I(inode)->refn;
  290. ORANGEFS_khandle_to(&refn.khandle, fh, 16);
  291. fh[4] = refn.fs_id;
  292. gossip_debug(GOSSIP_SUPER_DEBUG,
  293. "Encoding fh: handle %pU, fsid %u\n",
  294. &refn.khandle,
  295. refn.fs_id);
  296. if (parent) {
  297. refn = ORANGEFS_I(parent)->refn;
  298. ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
  299. fh[9] = refn.fs_id;
  300. type = 2;
  301. gossip_debug(GOSSIP_SUPER_DEBUG,
  302. "Encoding parent: handle %pU, fsid %u\n",
  303. &refn.khandle,
  304. refn.fs_id);
  305. }
  306. *max_len = len;
  307. out:
  308. return type;
  309. }
  310. static const struct export_operations orangefs_export_ops = {
  311. .encode_fh = orangefs_encode_fh,
  312. .fh_to_dentry = orangefs_fh_to_dentry,
  313. };
  314. static int orangefs_fill_sb(struct super_block *sb,
  315. struct orangefs_fs_mount_response *fs_mount,
  316. void *data, int silent)
  317. {
  318. int ret = -EINVAL;
  319. struct inode *root = NULL;
  320. struct dentry *root_dentry = NULL;
  321. struct orangefs_object_kref root_object;
  322. /* alloc and init our private orangefs sb info */
  323. sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
  324. if (!ORANGEFS_SB(sb))
  325. return -ENOMEM;
  326. ORANGEFS_SB(sb)->sb = sb;
  327. ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
  328. ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
  329. ORANGEFS_SB(sb)->id = fs_mount->id;
  330. if (data) {
  331. ret = parse_mount_options(sb, data, silent);
  332. if (ret)
  333. return ret;
  334. }
  335. /* Hang the xattr handlers off the superblock */
  336. sb->s_xattr = orangefs_xattr_handlers;
  337. sb->s_magic = ORANGEFS_SUPER_MAGIC;
  338. sb->s_op = &orangefs_s_ops;
  339. sb->s_d_op = &orangefs_dentry_operations;
  340. sb->s_blocksize = orangefs_bufmap_size_query();
  341. sb->s_blocksize_bits = orangefs_bufmap_shift_query();
  342. sb->s_maxbytes = MAX_LFS_FILESIZE;
  343. root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
  344. root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
  345. gossip_debug(GOSSIP_SUPER_DEBUG,
  346. "get inode %pU, fsid %d\n",
  347. &root_object.khandle,
  348. root_object.fs_id);
  349. root = orangefs_iget(sb, &root_object);
  350. if (IS_ERR(root))
  351. return PTR_ERR(root);
  352. gossip_debug(GOSSIP_SUPER_DEBUG,
  353. "Allocated root inode [%p] with mode %x\n",
  354. root,
  355. root->i_mode);
  356. /* allocates and places root dentry in dcache */
  357. root_dentry = d_make_root(root);
  358. if (!root_dentry)
  359. return -ENOMEM;
  360. sb->s_export_op = &orangefs_export_ops;
  361. sb->s_root = root_dentry;
  362. return 0;
  363. }
  364. struct dentry *orangefs_mount(struct file_system_type *fst,
  365. int flags,
  366. const char *devname,
  367. void *data)
  368. {
  369. int ret = -EINVAL;
  370. struct super_block *sb = ERR_PTR(-EINVAL);
  371. struct orangefs_kernel_op_s *new_op;
  372. struct dentry *d = ERR_PTR(-EINVAL);
  373. gossip_debug(GOSSIP_SUPER_DEBUG,
  374. "orangefs_mount: called with devname %s\n",
  375. devname);
  376. if (!devname) {
  377. gossip_err("ERROR: device name not specified.\n");
  378. return ERR_PTR(-EINVAL);
  379. }
  380. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  381. if (!new_op)
  382. return ERR_PTR(-ENOMEM);
  383. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  384. devname,
  385. ORANGEFS_MAX_SERVER_ADDR_LEN);
  386. gossip_debug(GOSSIP_SUPER_DEBUG,
  387. "Attempting ORANGEFS Mount via host %s\n",
  388. new_op->upcall.req.fs_mount.orangefs_config_server);
  389. ret = service_operation(new_op, "orangefs_mount", 0);
  390. gossip_debug(GOSSIP_SUPER_DEBUG,
  391. "orangefs_mount: mount got return value of %d\n", ret);
  392. if (ret)
  393. goto free_op;
  394. if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
  395. gossip_err("ERROR: Retrieved null fs_id\n");
  396. ret = -EINVAL;
  397. goto free_op;
  398. }
  399. sb = sget(fst, NULL, set_anon_super, flags, NULL);
  400. if (IS_ERR(sb)) {
  401. d = ERR_CAST(sb);
  402. goto free_op;
  403. }
  404. ret = orangefs_fill_sb(sb,
  405. &new_op->downcall.resp.fs_mount, data,
  406. flags & MS_SILENT ? 1 : 0);
  407. if (ret) {
  408. d = ERR_PTR(ret);
  409. goto free_sb_and_op;
  410. }
  411. /*
  412. * on successful mount, store the devname and data
  413. * used
  414. */
  415. strncpy(ORANGEFS_SB(sb)->devname,
  416. devname,
  417. ORANGEFS_MAX_SERVER_ADDR_LEN);
  418. /* mount_pending must be cleared */
  419. ORANGEFS_SB(sb)->mount_pending = 0;
  420. /*
  421. * finally, add this sb to our list of known orangefs
  422. * sb's
  423. */
  424. gossip_debug(GOSSIP_SUPER_DEBUG,
  425. "Adding SB %p to orangefs superblocks\n",
  426. ORANGEFS_SB(sb));
  427. spin_lock(&orangefs_superblocks_lock);
  428. list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
  429. spin_unlock(&orangefs_superblocks_lock);
  430. op_release(new_op);
  431. /* Must be removed from the list now. */
  432. ORANGEFS_SB(sb)->no_list = 0;
  433. if (orangefs_userspace_version >= 20906) {
  434. new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES);
  435. if (!new_op)
  436. return ERR_PTR(-ENOMEM);
  437. new_op->upcall.req.features.features = 0;
  438. ret = service_operation(new_op, "orangefs_features", 0);
  439. orangefs_features = new_op->downcall.resp.features.features;
  440. op_release(new_op);
  441. } else {
  442. orangefs_features = 0;
  443. }
  444. return dget(sb->s_root);
  445. free_sb_and_op:
  446. /* Will call orangefs_kill_sb with sb not in list. */
  447. ORANGEFS_SB(sb)->no_list = 1;
  448. deactivate_locked_super(sb);
  449. free_op:
  450. gossip_err("orangefs_mount: mount request failed with %d\n", ret);
  451. if (ret == -EINVAL) {
  452. gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
  453. gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
  454. }
  455. op_release(new_op);
  456. return d;
  457. }
  458. void orangefs_kill_sb(struct super_block *sb)
  459. {
  460. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
  461. /* provided sb cleanup */
  462. kill_anon_super(sb);
  463. if (!ORANGEFS_SB(sb)) {
  464. mutex_lock(&orangefs_request_mutex);
  465. mutex_unlock(&orangefs_request_mutex);
  466. return;
  467. }
  468. /*
  469. * issue the unmount to userspace to tell it to remove the
  470. * dynamic mount info it has for this superblock
  471. */
  472. orangefs_unmount_sb(sb);
  473. if (!ORANGEFS_SB(sb)->no_list) {
  474. /* remove the sb from our list of orangefs specific sb's */
  475. spin_lock(&orangefs_superblocks_lock);
  476. /* not list_del_init */
  477. __list_del_entry(&ORANGEFS_SB(sb)->list);
  478. ORANGEFS_SB(sb)->list.prev = NULL;
  479. spin_unlock(&orangefs_superblocks_lock);
  480. }
  481. /*
  482. * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
  483. * gets completed before we free the dang thing.
  484. */
  485. mutex_lock(&orangefs_request_mutex);
  486. mutex_unlock(&orangefs_request_mutex);
  487. /* free the orangefs superblock private data */
  488. kfree(ORANGEFS_SB(sb));
  489. }
  490. int orangefs_inode_cache_initialize(void)
  491. {
  492. orangefs_inode_cache = kmem_cache_create("orangefs_inode_cache",
  493. sizeof(struct orangefs_inode_s),
  494. 0,
  495. ORANGEFS_CACHE_CREATE_FLAGS,
  496. orangefs_inode_cache_ctor);
  497. if (!orangefs_inode_cache) {
  498. gossip_err("Cannot create orangefs_inode_cache\n");
  499. return -ENOMEM;
  500. }
  501. return 0;
  502. }
  503. int orangefs_inode_cache_finalize(void)
  504. {
  505. kmem_cache_destroy(orangefs_inode_cache);
  506. return 0;
  507. }