super.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. * Portions Copyright (C) Christoph Hellwig, 2001-2002
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/module.h>
  21. #include <linux/parser.h>
  22. #include <linux/completion.h>
  23. #include <linux/vfs.h>
  24. #include <linux/quotaops.h>
  25. #include <linux/mount.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/kthread.h>
  28. #include <linux/posix_acl.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/crc32.h>
  32. #include <linux/slab.h>
  33. #include <asm/uaccess.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/blkdev.h>
  36. #include "jfs_incore.h"
  37. #include "jfs_filsys.h"
  38. #include "jfs_inode.h"
  39. #include "jfs_metapage.h"
  40. #include "jfs_superblock.h"
  41. #include "jfs_dmap.h"
  42. #include "jfs_imap.h"
  43. #include "jfs_acl.h"
  44. #include "jfs_debug.h"
  45. #include "jfs_xattr.h"
  46. MODULE_DESCRIPTION("The Journaled Filesystem (JFS)");
  47. MODULE_AUTHOR("Steve Best/Dave Kleikamp/Barry Arndt, IBM");
  48. MODULE_LICENSE("GPL");
  49. static struct kmem_cache *jfs_inode_cachep;
  50. static const struct super_operations jfs_super_operations;
  51. static const struct export_operations jfs_export_operations;
  52. static struct file_system_type jfs_fs_type;
  53. #define MAX_COMMIT_THREADS 64
  54. static int commit_threads;
  55. module_param(commit_threads, int, 0);
  56. MODULE_PARM_DESC(commit_threads, "Number of commit threads");
  57. static struct task_struct *jfsCommitThread[MAX_COMMIT_THREADS];
  58. struct task_struct *jfsIOthread;
  59. struct task_struct *jfsSyncThread;
  60. #ifdef CONFIG_JFS_DEBUG
  61. int jfsloglevel = JFS_LOGLEVEL_WARN;
  62. module_param(jfsloglevel, int, 0644);
  63. MODULE_PARM_DESC(jfsloglevel, "Specify JFS loglevel (0, 1 or 2)");
  64. #endif
  65. static void jfs_handle_error(struct super_block *sb)
  66. {
  67. struct jfs_sb_info *sbi = JFS_SBI(sb);
  68. if (sb->s_flags & MS_RDONLY)
  69. return;
  70. updateSuper(sb, FM_DIRTY);
  71. if (sbi->flag & JFS_ERR_PANIC)
  72. panic("JFS (device %s): panic forced after error\n",
  73. sb->s_id);
  74. else if (sbi->flag & JFS_ERR_REMOUNT_RO) {
  75. jfs_err("ERROR: (device %s): remounting filesystem as read-only\n",
  76. sb->s_id);
  77. sb->s_flags |= MS_RDONLY;
  78. }
  79. /* nothing is done for continue beyond marking the superblock dirty */
  80. }
  81. void jfs_error(struct super_block *sb, const char *fmt, ...)
  82. {
  83. struct va_format vaf;
  84. va_list args;
  85. va_start(args, fmt);
  86. vaf.fmt = fmt;
  87. vaf.va = &args;
  88. pr_err("ERROR: (device %s): %pf: %pV\n",
  89. sb->s_id, __builtin_return_address(0), &vaf);
  90. va_end(args);
  91. jfs_handle_error(sb);
  92. }
  93. static struct inode *jfs_alloc_inode(struct super_block *sb)
  94. {
  95. struct jfs_inode_info *jfs_inode;
  96. jfs_inode = kmem_cache_alloc(jfs_inode_cachep, GFP_NOFS);
  97. if (!jfs_inode)
  98. return NULL;
  99. return &jfs_inode->vfs_inode;
  100. }
  101. static void jfs_i_callback(struct rcu_head *head)
  102. {
  103. struct inode *inode = container_of(head, struct inode, i_rcu);
  104. struct jfs_inode_info *ji = JFS_IP(inode);
  105. kmem_cache_free(jfs_inode_cachep, ji);
  106. }
  107. static void jfs_destroy_inode(struct inode *inode)
  108. {
  109. struct jfs_inode_info *ji = JFS_IP(inode);
  110. BUG_ON(!list_empty(&ji->anon_inode_list));
  111. spin_lock_irq(&ji->ag_lock);
  112. if (ji->active_ag != -1) {
  113. struct bmap *bmap = JFS_SBI(inode->i_sb)->bmap;
  114. atomic_dec(&bmap->db_active[ji->active_ag]);
  115. ji->active_ag = -1;
  116. }
  117. spin_unlock_irq(&ji->ag_lock);
  118. call_rcu(&inode->i_rcu, jfs_i_callback);
  119. }
  120. static int jfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  121. {
  122. struct jfs_sb_info *sbi = JFS_SBI(dentry->d_sb);
  123. s64 maxinodes;
  124. struct inomap *imap = JFS_IP(sbi->ipimap)->i_imap;
  125. jfs_info("In jfs_statfs");
  126. buf->f_type = JFS_SUPER_MAGIC;
  127. buf->f_bsize = sbi->bsize;
  128. buf->f_blocks = sbi->bmap->db_mapsize;
  129. buf->f_bfree = sbi->bmap->db_nfree;
  130. buf->f_bavail = sbi->bmap->db_nfree;
  131. /*
  132. * If we really return the number of allocated & free inodes, some
  133. * applications will fail because they won't see enough free inodes.
  134. * We'll try to calculate some guess as to how many inodes we can
  135. * really allocate
  136. *
  137. * buf->f_files = atomic_read(&imap->im_numinos);
  138. * buf->f_ffree = atomic_read(&imap->im_numfree);
  139. */
  140. maxinodes = min((s64) atomic_read(&imap->im_numinos) +
  141. ((sbi->bmap->db_nfree >> imap->im_l2nbperiext)
  142. << L2INOSPEREXT), (s64) 0xffffffffLL);
  143. buf->f_files = maxinodes;
  144. buf->f_ffree = maxinodes - (atomic_read(&imap->im_numinos) -
  145. atomic_read(&imap->im_numfree));
  146. buf->f_fsid.val[0] = (u32)crc32_le(0, sbi->uuid, sizeof(sbi->uuid)/2);
  147. buf->f_fsid.val[1] = (u32)crc32_le(0, sbi->uuid + sizeof(sbi->uuid)/2,
  148. sizeof(sbi->uuid)/2);
  149. buf->f_namelen = JFS_NAME_MAX;
  150. return 0;
  151. }
  152. static void jfs_put_super(struct super_block *sb)
  153. {
  154. struct jfs_sb_info *sbi = JFS_SBI(sb);
  155. int rc;
  156. jfs_info("In jfs_put_super");
  157. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  158. rc = jfs_umount(sb);
  159. if (rc)
  160. jfs_err("jfs_umount failed with return code %d", rc);
  161. unload_nls(sbi->nls_tab);
  162. truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
  163. iput(sbi->direct_inode);
  164. kfree(sbi);
  165. }
  166. enum {
  167. Opt_integrity, Opt_nointegrity, Opt_iocharset, Opt_resize,
  168. Opt_resize_nosize, Opt_errors, Opt_ignore, Opt_err, Opt_quota,
  169. Opt_usrquota, Opt_grpquota, Opt_uid, Opt_gid, Opt_umask,
  170. Opt_discard, Opt_nodiscard, Opt_discard_minblk
  171. };
  172. static const match_table_t tokens = {
  173. {Opt_integrity, "integrity"},
  174. {Opt_nointegrity, "nointegrity"},
  175. {Opt_iocharset, "iocharset=%s"},
  176. {Opt_resize, "resize=%u"},
  177. {Opt_resize_nosize, "resize"},
  178. {Opt_errors, "errors=%s"},
  179. {Opt_ignore, "noquota"},
  180. {Opt_ignore, "quota"},
  181. {Opt_usrquota, "usrquota"},
  182. {Opt_grpquota, "grpquota"},
  183. {Opt_uid, "uid=%u"},
  184. {Opt_gid, "gid=%u"},
  185. {Opt_umask, "umask=%u"},
  186. {Opt_discard, "discard"},
  187. {Opt_nodiscard, "nodiscard"},
  188. {Opt_discard_minblk, "discard=%u"},
  189. {Opt_err, NULL}
  190. };
  191. static int parse_options(char *options, struct super_block *sb, s64 *newLVSize,
  192. int *flag)
  193. {
  194. void *nls_map = (void *)-1; /* -1: no change; NULL: none */
  195. char *p;
  196. struct jfs_sb_info *sbi = JFS_SBI(sb);
  197. *newLVSize = 0;
  198. if (!options)
  199. return 1;
  200. while ((p = strsep(&options, ",")) != NULL) {
  201. substring_t args[MAX_OPT_ARGS];
  202. int token;
  203. if (!*p)
  204. continue;
  205. token = match_token(p, tokens, args);
  206. switch (token) {
  207. case Opt_integrity:
  208. *flag &= ~JFS_NOINTEGRITY;
  209. break;
  210. case Opt_nointegrity:
  211. *flag |= JFS_NOINTEGRITY;
  212. break;
  213. case Opt_ignore:
  214. /* Silently ignore the quota options */
  215. /* Don't do anything ;-) */
  216. break;
  217. case Opt_iocharset:
  218. if (nls_map && nls_map != (void *) -1)
  219. unload_nls(nls_map);
  220. if (!strcmp(args[0].from, "none"))
  221. nls_map = NULL;
  222. else {
  223. nls_map = load_nls(args[0].from);
  224. if (!nls_map) {
  225. pr_err("JFS: charset not found\n");
  226. goto cleanup;
  227. }
  228. }
  229. break;
  230. case Opt_resize:
  231. {
  232. char *resize = args[0].from;
  233. int rc = kstrtoll(resize, 0, newLVSize);
  234. if (rc)
  235. goto cleanup;
  236. break;
  237. }
  238. case Opt_resize_nosize:
  239. {
  240. *newLVSize = sb->s_bdev->bd_inode->i_size >>
  241. sb->s_blocksize_bits;
  242. if (*newLVSize == 0)
  243. pr_err("JFS: Cannot determine volume size\n");
  244. break;
  245. }
  246. case Opt_errors:
  247. {
  248. char *errors = args[0].from;
  249. if (!errors || !*errors)
  250. goto cleanup;
  251. if (!strcmp(errors, "continue")) {
  252. *flag &= ~JFS_ERR_REMOUNT_RO;
  253. *flag &= ~JFS_ERR_PANIC;
  254. *flag |= JFS_ERR_CONTINUE;
  255. } else if (!strcmp(errors, "remount-ro")) {
  256. *flag &= ~JFS_ERR_CONTINUE;
  257. *flag &= ~JFS_ERR_PANIC;
  258. *flag |= JFS_ERR_REMOUNT_RO;
  259. } else if (!strcmp(errors, "panic")) {
  260. *flag &= ~JFS_ERR_CONTINUE;
  261. *flag &= ~JFS_ERR_REMOUNT_RO;
  262. *flag |= JFS_ERR_PANIC;
  263. } else {
  264. pr_err("JFS: %s is an invalid error handler\n",
  265. errors);
  266. goto cleanup;
  267. }
  268. break;
  269. }
  270. #ifdef CONFIG_QUOTA
  271. case Opt_quota:
  272. case Opt_usrquota:
  273. *flag |= JFS_USRQUOTA;
  274. break;
  275. case Opt_grpquota:
  276. *flag |= JFS_GRPQUOTA;
  277. break;
  278. #else
  279. case Opt_usrquota:
  280. case Opt_grpquota:
  281. case Opt_quota:
  282. pr_err("JFS: quota operations not supported\n");
  283. break;
  284. #endif
  285. case Opt_uid:
  286. {
  287. char *uid = args[0].from;
  288. uid_t val;
  289. int rc = kstrtouint(uid, 0, &val);
  290. if (rc)
  291. goto cleanup;
  292. sbi->uid = make_kuid(current_user_ns(), val);
  293. if (!uid_valid(sbi->uid))
  294. goto cleanup;
  295. break;
  296. }
  297. case Opt_gid:
  298. {
  299. char *gid = args[0].from;
  300. gid_t val;
  301. int rc = kstrtouint(gid, 0, &val);
  302. if (rc)
  303. goto cleanup;
  304. sbi->gid = make_kgid(current_user_ns(), val);
  305. if (!gid_valid(sbi->gid))
  306. goto cleanup;
  307. break;
  308. }
  309. case Opt_umask:
  310. {
  311. char *umask = args[0].from;
  312. int rc = kstrtouint(umask, 8, &sbi->umask);
  313. if (rc)
  314. goto cleanup;
  315. if (sbi->umask & ~0777) {
  316. pr_err("JFS: Invalid value of umask\n");
  317. goto cleanup;
  318. }
  319. break;
  320. }
  321. case Opt_discard:
  322. {
  323. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  324. /* if set to 1, even copying files will cause
  325. * trimming :O
  326. * -> user has more control over the online trimming
  327. */
  328. sbi->minblks_trim = 64;
  329. if (blk_queue_discard(q))
  330. *flag |= JFS_DISCARD;
  331. else
  332. pr_err("JFS: discard option not supported on device\n");
  333. break;
  334. }
  335. case Opt_nodiscard:
  336. *flag &= ~JFS_DISCARD;
  337. break;
  338. case Opt_discard_minblk:
  339. {
  340. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  341. char *minblks_trim = args[0].from;
  342. int rc;
  343. if (blk_queue_discard(q)) {
  344. *flag |= JFS_DISCARD;
  345. rc = kstrtouint(minblks_trim, 0,
  346. &sbi->minblks_trim);
  347. if (rc)
  348. goto cleanup;
  349. } else
  350. pr_err("JFS: discard option not supported on device\n");
  351. break;
  352. }
  353. default:
  354. printk("jfs: Unrecognized mount option \"%s\" or missing value\n",
  355. p);
  356. goto cleanup;
  357. }
  358. }
  359. if (nls_map != (void *) -1) {
  360. /* Discard old (if remount) */
  361. unload_nls(sbi->nls_tab);
  362. sbi->nls_tab = nls_map;
  363. }
  364. return 1;
  365. cleanup:
  366. if (nls_map && nls_map != (void *) -1)
  367. unload_nls(nls_map);
  368. return 0;
  369. }
  370. static int jfs_remount(struct super_block *sb, int *flags, char *data)
  371. {
  372. s64 newLVSize = 0;
  373. int rc = 0;
  374. int flag = JFS_SBI(sb)->flag;
  375. int ret;
  376. sync_filesystem(sb);
  377. if (!parse_options(data, sb, &newLVSize, &flag))
  378. return -EINVAL;
  379. if (newLVSize) {
  380. if (sb->s_flags & MS_RDONLY) {
  381. pr_err("JFS: resize requires volume to be mounted read-write\n");
  382. return -EROFS;
  383. }
  384. rc = jfs_extendfs(sb, newLVSize, 0);
  385. if (rc)
  386. return rc;
  387. }
  388. if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
  389. /*
  390. * Invalidate any previously read metadata. fsck may have
  391. * changed the on-disk data since we mounted r/o
  392. */
  393. truncate_inode_pages(JFS_SBI(sb)->direct_inode->i_mapping, 0);
  394. JFS_SBI(sb)->flag = flag;
  395. ret = jfs_mount_rw(sb, 1);
  396. /* mark the fs r/w for quota activity */
  397. sb->s_flags &= ~MS_RDONLY;
  398. dquot_resume(sb, -1);
  399. return ret;
  400. }
  401. if ((!(sb->s_flags & MS_RDONLY)) && (*flags & MS_RDONLY)) {
  402. rc = dquot_suspend(sb, -1);
  403. if (rc < 0)
  404. return rc;
  405. rc = jfs_umount_rw(sb);
  406. JFS_SBI(sb)->flag = flag;
  407. return rc;
  408. }
  409. if ((JFS_SBI(sb)->flag & JFS_NOINTEGRITY) != (flag & JFS_NOINTEGRITY))
  410. if (!(sb->s_flags & MS_RDONLY)) {
  411. rc = jfs_umount_rw(sb);
  412. if (rc)
  413. return rc;
  414. JFS_SBI(sb)->flag = flag;
  415. ret = jfs_mount_rw(sb, 1);
  416. return ret;
  417. }
  418. JFS_SBI(sb)->flag = flag;
  419. return 0;
  420. }
  421. static int jfs_fill_super(struct super_block *sb, void *data, int silent)
  422. {
  423. struct jfs_sb_info *sbi;
  424. struct inode *inode;
  425. int rc;
  426. s64 newLVSize = 0;
  427. int flag, ret = -EINVAL;
  428. jfs_info("In jfs_read_super: s_flags=0x%lx", sb->s_flags);
  429. if (!new_valid_dev(sb->s_bdev->bd_dev))
  430. return -EOVERFLOW;
  431. sbi = kzalloc(sizeof(struct jfs_sb_info), GFP_KERNEL);
  432. if (!sbi)
  433. return -ENOMEM;
  434. sb->s_fs_info = sbi;
  435. sb->s_max_links = JFS_LINK_MAX;
  436. sbi->sb = sb;
  437. sbi->uid = INVALID_UID;
  438. sbi->gid = INVALID_GID;
  439. sbi->umask = -1;
  440. /* initialize the mount flag and determine the default error handler */
  441. flag = JFS_ERR_REMOUNT_RO;
  442. if (!parse_options((char *) data, sb, &newLVSize, &flag))
  443. goto out_kfree;
  444. sbi->flag = flag;
  445. #ifdef CONFIG_JFS_POSIX_ACL
  446. sb->s_flags |= MS_POSIXACL;
  447. #endif
  448. if (newLVSize) {
  449. pr_err("resize option for remount only\n");
  450. goto out_kfree;
  451. }
  452. /*
  453. * Initialize blocksize to 4K.
  454. */
  455. sb_set_blocksize(sb, PSIZE);
  456. /*
  457. * Set method vectors.
  458. */
  459. sb->s_op = &jfs_super_operations;
  460. sb->s_export_op = &jfs_export_operations;
  461. sb->s_xattr = jfs_xattr_handlers;
  462. #ifdef CONFIG_QUOTA
  463. sb->dq_op = &dquot_operations;
  464. sb->s_qcop = &dquot_quotactl_ops;
  465. #endif
  466. /*
  467. * Initialize direct-mapping inode/address-space
  468. */
  469. inode = new_inode(sb);
  470. if (inode == NULL) {
  471. ret = -ENOMEM;
  472. goto out_unload;
  473. }
  474. inode->i_ino = 0;
  475. inode->i_size = sb->s_bdev->bd_inode->i_size;
  476. inode->i_mapping->a_ops = &jfs_metapage_aops;
  477. insert_inode_hash(inode);
  478. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  479. sbi->direct_inode = inode;
  480. rc = jfs_mount(sb);
  481. if (rc) {
  482. if (!silent)
  483. jfs_err("jfs_mount failed w/return code = %d", rc);
  484. goto out_mount_failed;
  485. }
  486. if (sb->s_flags & MS_RDONLY)
  487. sbi->log = NULL;
  488. else {
  489. rc = jfs_mount_rw(sb, 0);
  490. if (rc) {
  491. if (!silent) {
  492. jfs_err("jfs_mount_rw failed, return code = %d",
  493. rc);
  494. }
  495. goto out_no_rw;
  496. }
  497. }
  498. sb->s_magic = JFS_SUPER_MAGIC;
  499. if (sbi->mntflag & JFS_OS2)
  500. sb->s_d_op = &jfs_ci_dentry_operations;
  501. inode = jfs_iget(sb, ROOT_I);
  502. if (IS_ERR(inode)) {
  503. ret = PTR_ERR(inode);
  504. goto out_no_rw;
  505. }
  506. sb->s_root = d_make_root(inode);
  507. if (!sb->s_root)
  508. goto out_no_root;
  509. /* logical blocks are represented by 40 bits in pxd_t, etc. */
  510. sb->s_maxbytes = ((u64) sb->s_blocksize) << 40;
  511. #if BITS_PER_LONG == 32
  512. /*
  513. * Page cache is indexed by long.
  514. * I would use MAX_LFS_FILESIZE, but it's only half as big
  515. */
  516. sb->s_maxbytes = min(((u64) PAGE_CACHE_SIZE << 32) - 1,
  517. (u64)sb->s_maxbytes);
  518. #endif
  519. sb->s_time_gran = 1;
  520. return 0;
  521. out_no_root:
  522. jfs_err("jfs_read_super: get root dentry failed");
  523. out_no_rw:
  524. rc = jfs_umount(sb);
  525. if (rc)
  526. jfs_err("jfs_umount failed with return code %d", rc);
  527. out_mount_failed:
  528. filemap_write_and_wait(sbi->direct_inode->i_mapping);
  529. truncate_inode_pages(sbi->direct_inode->i_mapping, 0);
  530. make_bad_inode(sbi->direct_inode);
  531. iput(sbi->direct_inode);
  532. sbi->direct_inode = NULL;
  533. out_unload:
  534. if (sbi->nls_tab)
  535. unload_nls(sbi->nls_tab);
  536. out_kfree:
  537. kfree(sbi);
  538. return ret;
  539. }
  540. static int jfs_freeze(struct super_block *sb)
  541. {
  542. struct jfs_sb_info *sbi = JFS_SBI(sb);
  543. struct jfs_log *log = sbi->log;
  544. int rc = 0;
  545. if (!(sb->s_flags & MS_RDONLY)) {
  546. txQuiesce(sb);
  547. rc = lmLogShutdown(log);
  548. if (rc) {
  549. jfs_error(sb, "lmLogShutdown failed\n");
  550. /* let operations fail rather than hang */
  551. txResume(sb);
  552. return rc;
  553. }
  554. rc = updateSuper(sb, FM_CLEAN);
  555. if (rc) {
  556. jfs_err("jfs_freeze: updateSuper failed\n");
  557. /*
  558. * Don't fail here. Everything succeeded except
  559. * marking the superblock clean, so there's really
  560. * no harm in leaving it frozen for now.
  561. */
  562. }
  563. }
  564. return 0;
  565. }
  566. static int jfs_unfreeze(struct super_block *sb)
  567. {
  568. struct jfs_sb_info *sbi = JFS_SBI(sb);
  569. struct jfs_log *log = sbi->log;
  570. int rc = 0;
  571. if (!(sb->s_flags & MS_RDONLY)) {
  572. rc = updateSuper(sb, FM_MOUNT);
  573. if (rc) {
  574. jfs_error(sb, "updateSuper failed\n");
  575. goto out;
  576. }
  577. rc = lmLogInit(log);
  578. if (rc)
  579. jfs_error(sb, "lmLogInit failed\n");
  580. out:
  581. txResume(sb);
  582. }
  583. return rc;
  584. }
  585. static struct dentry *jfs_do_mount(struct file_system_type *fs_type,
  586. int flags, const char *dev_name, void *data)
  587. {
  588. return mount_bdev(fs_type, flags, dev_name, data, jfs_fill_super);
  589. }
  590. static int jfs_sync_fs(struct super_block *sb, int wait)
  591. {
  592. struct jfs_log *log = JFS_SBI(sb)->log;
  593. /* log == NULL indicates read-only mount */
  594. if (log) {
  595. /*
  596. * Write quota structures to quota file, sync_blockdev() will
  597. * write them to disk later
  598. */
  599. dquot_writeback_dquots(sb, -1);
  600. jfs_flush_journal(log, wait);
  601. jfs_syncpt(log, 0);
  602. }
  603. return 0;
  604. }
  605. static int jfs_show_options(struct seq_file *seq, struct dentry *root)
  606. {
  607. struct jfs_sb_info *sbi = JFS_SBI(root->d_sb);
  608. if (uid_valid(sbi->uid))
  609. seq_printf(seq, ",uid=%d", from_kuid(&init_user_ns, sbi->uid));
  610. if (gid_valid(sbi->gid))
  611. seq_printf(seq, ",gid=%d", from_kgid(&init_user_ns, sbi->gid));
  612. if (sbi->umask != -1)
  613. seq_printf(seq, ",umask=%03o", sbi->umask);
  614. if (sbi->flag & JFS_NOINTEGRITY)
  615. seq_puts(seq, ",nointegrity");
  616. if (sbi->flag & JFS_DISCARD)
  617. seq_printf(seq, ",discard=%u", sbi->minblks_trim);
  618. if (sbi->nls_tab)
  619. seq_printf(seq, ",iocharset=%s", sbi->nls_tab->charset);
  620. if (sbi->flag & JFS_ERR_CONTINUE)
  621. seq_printf(seq, ",errors=continue");
  622. if (sbi->flag & JFS_ERR_PANIC)
  623. seq_printf(seq, ",errors=panic");
  624. #ifdef CONFIG_QUOTA
  625. if (sbi->flag & JFS_USRQUOTA)
  626. seq_puts(seq, ",usrquota");
  627. if (sbi->flag & JFS_GRPQUOTA)
  628. seq_puts(seq, ",grpquota");
  629. #endif
  630. return 0;
  631. }
  632. #ifdef CONFIG_QUOTA
  633. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  634. * acquiring the locks... As quota files are never truncated and quota code
  635. * itself serializes the operations (and no one else should touch the files)
  636. * we don't have to be afraid of races */
  637. static ssize_t jfs_quota_read(struct super_block *sb, int type, char *data,
  638. size_t len, loff_t off)
  639. {
  640. struct inode *inode = sb_dqopt(sb)->files[type];
  641. sector_t blk = off >> sb->s_blocksize_bits;
  642. int err = 0;
  643. int offset = off & (sb->s_blocksize - 1);
  644. int tocopy;
  645. size_t toread;
  646. struct buffer_head tmp_bh;
  647. struct buffer_head *bh;
  648. loff_t i_size = i_size_read(inode);
  649. if (off > i_size)
  650. return 0;
  651. if (off+len > i_size)
  652. len = i_size-off;
  653. toread = len;
  654. while (toread > 0) {
  655. tocopy = sb->s_blocksize - offset < toread ?
  656. sb->s_blocksize - offset : toread;
  657. tmp_bh.b_state = 0;
  658. tmp_bh.b_size = 1 << inode->i_blkbits;
  659. err = jfs_get_block(inode, blk, &tmp_bh, 0);
  660. if (err)
  661. return err;
  662. if (!buffer_mapped(&tmp_bh)) /* A hole? */
  663. memset(data, 0, tocopy);
  664. else {
  665. bh = sb_bread(sb, tmp_bh.b_blocknr);
  666. if (!bh)
  667. return -EIO;
  668. memcpy(data, bh->b_data+offset, tocopy);
  669. brelse(bh);
  670. }
  671. offset = 0;
  672. toread -= tocopy;
  673. data += tocopy;
  674. blk++;
  675. }
  676. return len;
  677. }
  678. /* Write to quotafile */
  679. static ssize_t jfs_quota_write(struct super_block *sb, int type,
  680. const char *data, size_t len, loff_t off)
  681. {
  682. struct inode *inode = sb_dqopt(sb)->files[type];
  683. sector_t blk = off >> sb->s_blocksize_bits;
  684. int err = 0;
  685. int offset = off & (sb->s_blocksize - 1);
  686. int tocopy;
  687. size_t towrite = len;
  688. struct buffer_head tmp_bh;
  689. struct buffer_head *bh;
  690. mutex_lock(&inode->i_mutex);
  691. while (towrite > 0) {
  692. tocopy = sb->s_blocksize - offset < towrite ?
  693. sb->s_blocksize - offset : towrite;
  694. tmp_bh.b_state = 0;
  695. tmp_bh.b_size = 1 << inode->i_blkbits;
  696. err = jfs_get_block(inode, blk, &tmp_bh, 1);
  697. if (err)
  698. goto out;
  699. if (offset || tocopy != sb->s_blocksize)
  700. bh = sb_bread(sb, tmp_bh.b_blocknr);
  701. else
  702. bh = sb_getblk(sb, tmp_bh.b_blocknr);
  703. if (!bh) {
  704. err = -EIO;
  705. goto out;
  706. }
  707. lock_buffer(bh);
  708. memcpy(bh->b_data+offset, data, tocopy);
  709. flush_dcache_page(bh->b_page);
  710. set_buffer_uptodate(bh);
  711. mark_buffer_dirty(bh);
  712. unlock_buffer(bh);
  713. brelse(bh);
  714. offset = 0;
  715. towrite -= tocopy;
  716. data += tocopy;
  717. blk++;
  718. }
  719. out:
  720. if (len == towrite) {
  721. mutex_unlock(&inode->i_mutex);
  722. return err;
  723. }
  724. if (inode->i_size < off+len-towrite)
  725. i_size_write(inode, off+len-towrite);
  726. inode->i_version++;
  727. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  728. mark_inode_dirty(inode);
  729. mutex_unlock(&inode->i_mutex);
  730. return len - towrite;
  731. }
  732. #endif
  733. static const struct super_operations jfs_super_operations = {
  734. .alloc_inode = jfs_alloc_inode,
  735. .destroy_inode = jfs_destroy_inode,
  736. .dirty_inode = jfs_dirty_inode,
  737. .write_inode = jfs_write_inode,
  738. .evict_inode = jfs_evict_inode,
  739. .put_super = jfs_put_super,
  740. .sync_fs = jfs_sync_fs,
  741. .freeze_fs = jfs_freeze,
  742. .unfreeze_fs = jfs_unfreeze,
  743. .statfs = jfs_statfs,
  744. .remount_fs = jfs_remount,
  745. .show_options = jfs_show_options,
  746. #ifdef CONFIG_QUOTA
  747. .quota_read = jfs_quota_read,
  748. .quota_write = jfs_quota_write,
  749. #endif
  750. };
  751. static const struct export_operations jfs_export_operations = {
  752. .fh_to_dentry = jfs_fh_to_dentry,
  753. .fh_to_parent = jfs_fh_to_parent,
  754. .get_parent = jfs_get_parent,
  755. };
  756. static struct file_system_type jfs_fs_type = {
  757. .owner = THIS_MODULE,
  758. .name = "jfs",
  759. .mount = jfs_do_mount,
  760. .kill_sb = kill_block_super,
  761. .fs_flags = FS_REQUIRES_DEV,
  762. };
  763. MODULE_ALIAS_FS("jfs");
  764. static void init_once(void *foo)
  765. {
  766. struct jfs_inode_info *jfs_ip = (struct jfs_inode_info *) foo;
  767. memset(jfs_ip, 0, sizeof(struct jfs_inode_info));
  768. INIT_LIST_HEAD(&jfs_ip->anon_inode_list);
  769. init_rwsem(&jfs_ip->rdwrlock);
  770. mutex_init(&jfs_ip->commit_mutex);
  771. init_rwsem(&jfs_ip->xattr_sem);
  772. spin_lock_init(&jfs_ip->ag_lock);
  773. jfs_ip->active_ag = -1;
  774. inode_init_once(&jfs_ip->vfs_inode);
  775. }
  776. static int __init init_jfs_fs(void)
  777. {
  778. int i;
  779. int rc;
  780. jfs_inode_cachep =
  781. kmem_cache_create("jfs_ip", sizeof(struct jfs_inode_info), 0,
  782. SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
  783. init_once);
  784. if (jfs_inode_cachep == NULL)
  785. return -ENOMEM;
  786. /*
  787. * Metapage initialization
  788. */
  789. rc = metapage_init();
  790. if (rc) {
  791. jfs_err("metapage_init failed w/rc = %d", rc);
  792. goto free_slab;
  793. }
  794. /*
  795. * Transaction Manager initialization
  796. */
  797. rc = txInit();
  798. if (rc) {
  799. jfs_err("txInit failed w/rc = %d", rc);
  800. goto free_metapage;
  801. }
  802. /*
  803. * I/O completion thread (endio)
  804. */
  805. jfsIOthread = kthread_run(jfsIOWait, NULL, "jfsIO");
  806. if (IS_ERR(jfsIOthread)) {
  807. rc = PTR_ERR(jfsIOthread);
  808. jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
  809. goto end_txmngr;
  810. }
  811. if (commit_threads < 1)
  812. commit_threads = num_online_cpus();
  813. if (commit_threads > MAX_COMMIT_THREADS)
  814. commit_threads = MAX_COMMIT_THREADS;
  815. for (i = 0; i < commit_threads; i++) {
  816. jfsCommitThread[i] = kthread_run(jfs_lazycommit, NULL,
  817. "jfsCommit");
  818. if (IS_ERR(jfsCommitThread[i])) {
  819. rc = PTR_ERR(jfsCommitThread[i]);
  820. jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
  821. commit_threads = i;
  822. goto kill_committask;
  823. }
  824. }
  825. jfsSyncThread = kthread_run(jfs_sync, NULL, "jfsSync");
  826. if (IS_ERR(jfsSyncThread)) {
  827. rc = PTR_ERR(jfsSyncThread);
  828. jfs_err("init_jfs_fs: fork failed w/rc = %d", rc);
  829. goto kill_committask;
  830. }
  831. #ifdef PROC_FS_JFS
  832. jfs_proc_init();
  833. #endif
  834. rc = register_filesystem(&jfs_fs_type);
  835. if (!rc)
  836. return 0;
  837. #ifdef PROC_FS_JFS
  838. jfs_proc_clean();
  839. #endif
  840. kthread_stop(jfsSyncThread);
  841. kill_committask:
  842. for (i = 0; i < commit_threads; i++)
  843. kthread_stop(jfsCommitThread[i]);
  844. kthread_stop(jfsIOthread);
  845. end_txmngr:
  846. txExit();
  847. free_metapage:
  848. metapage_exit();
  849. free_slab:
  850. kmem_cache_destroy(jfs_inode_cachep);
  851. return rc;
  852. }
  853. static void __exit exit_jfs_fs(void)
  854. {
  855. int i;
  856. jfs_info("exit_jfs_fs called");
  857. txExit();
  858. metapage_exit();
  859. kthread_stop(jfsIOthread);
  860. for (i = 0; i < commit_threads; i++)
  861. kthread_stop(jfsCommitThread[i]);
  862. kthread_stop(jfsSyncThread);
  863. #ifdef PROC_FS_JFS
  864. jfs_proc_clean();
  865. #endif
  866. unregister_filesystem(&jfs_fs_type);
  867. /*
  868. * Make sure all delayed rcu free inodes are flushed before we
  869. * destroy cache.
  870. */
  871. rcu_barrier();
  872. kmem_cache_destroy(jfs_inode_cachep);
  873. }
  874. module_init(init_jfs_fs)
  875. module_exit(exit_jfs_fs)