super.c 24 KB

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