inode.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/isofs/inode.c
  4. *
  5. * (C) 1991 Linus Torvalds - minix filesystem
  6. * 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem.
  7. * 1994 Eberhard Mönkeberg - multi session handling.
  8. * 1995 Mark Dobie - allow mounting of some weird VideoCDs and PhotoCDs.
  9. * 1997 Gordon Chaffee - Joliet CDs
  10. * 1998 Eric Lammerts - ISO 9660 Level 3
  11. * 2004 Paul Serice - Inode Support pushed out from 4GB to 128GB
  12. * 2004 Paul Serice - NFS Export Operations
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/cred.h>
  18. #include <linux/nls.h>
  19. #include <linux/ctype.h>
  20. #include <linux/statfs.h>
  21. #include <linux/cdrom.h>
  22. #include <linux/mpage.h>
  23. #include <linux/user_namespace.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/fs_context.h>
  27. #include <linux/fs_parser.h>
  28. #include "isofs.h"
  29. #include "zisofs.h"
  30. /* max tz offset is 13 hours */
  31. #define MAX_TZ_OFFSET (52*15*60)
  32. #define BEQUIET
  33. static int isofs_hashi(const struct dentry *parent, struct qstr *qstr);
  34. static int isofs_dentry_cmpi(const struct dentry *dentry,
  35. unsigned int len, const char *str, const struct qstr *name);
  36. #ifdef CONFIG_JOLIET
  37. static int isofs_hashi_ms(const struct dentry *parent, struct qstr *qstr);
  38. static int isofs_hash_ms(const struct dentry *parent, struct qstr *qstr);
  39. static int isofs_dentry_cmpi_ms(const struct dentry *dentry,
  40. unsigned int len, const char *str, const struct qstr *name);
  41. static int isofs_dentry_cmp_ms(const struct dentry *dentry,
  42. unsigned int len, const char *str, const struct qstr *name);
  43. #endif
  44. static void isofs_put_super(struct super_block *sb)
  45. {
  46. struct isofs_sb_info *sbi = ISOFS_SB(sb);
  47. #ifdef CONFIG_JOLIET
  48. unload_nls(sbi->s_nls_iocharset);
  49. #endif
  50. kfree(sbi);
  51. sb->s_fs_info = NULL;
  52. return;
  53. }
  54. static int isofs_read_inode(struct inode *, int relocated);
  55. static int isofs_statfs (struct dentry *, struct kstatfs *);
  56. static int isofs_show_options(struct seq_file *, struct dentry *);
  57. static struct kmem_cache *isofs_inode_cachep;
  58. static struct inode *isofs_alloc_inode(struct super_block *sb)
  59. {
  60. struct iso_inode_info *ei;
  61. ei = alloc_inode_sb(sb, isofs_inode_cachep, GFP_KERNEL);
  62. if (!ei)
  63. return NULL;
  64. return &ei->vfs_inode;
  65. }
  66. static void isofs_free_inode(struct inode *inode)
  67. {
  68. kmem_cache_free(isofs_inode_cachep, ISOFS_I(inode));
  69. }
  70. static void init_once(void *foo)
  71. {
  72. struct iso_inode_info *ei = foo;
  73. inode_init_once(&ei->vfs_inode);
  74. }
  75. static int __init init_inodecache(void)
  76. {
  77. isofs_inode_cachep = kmem_cache_create("isofs_inode_cache",
  78. sizeof(struct iso_inode_info),
  79. 0, (SLAB_RECLAIM_ACCOUNT|
  80. SLAB_ACCOUNT),
  81. init_once);
  82. if (!isofs_inode_cachep)
  83. return -ENOMEM;
  84. return 0;
  85. }
  86. static void destroy_inodecache(void)
  87. {
  88. /*
  89. * Make sure all delayed rcu free inodes are flushed before we
  90. * destroy cache.
  91. */
  92. rcu_barrier();
  93. kmem_cache_destroy(isofs_inode_cachep);
  94. }
  95. static int isofs_reconfigure(struct fs_context *fc)
  96. {
  97. sync_filesystem(fc->root->d_sb);
  98. if (!(fc->sb_flags & SB_RDONLY))
  99. return -EROFS;
  100. return 0;
  101. }
  102. static const struct super_operations isofs_sops = {
  103. .alloc_inode = isofs_alloc_inode,
  104. .free_inode = isofs_free_inode,
  105. .put_super = isofs_put_super,
  106. .statfs = isofs_statfs,
  107. .show_options = isofs_show_options,
  108. };
  109. static const struct dentry_operations isofs_dentry_ops[] = {
  110. {
  111. .d_hash = isofs_hashi,
  112. .d_compare = isofs_dentry_cmpi,
  113. },
  114. #ifdef CONFIG_JOLIET
  115. {
  116. .d_hash = isofs_hash_ms,
  117. .d_compare = isofs_dentry_cmp_ms,
  118. },
  119. {
  120. .d_hash = isofs_hashi_ms,
  121. .d_compare = isofs_dentry_cmpi_ms,
  122. },
  123. #endif
  124. };
  125. struct isofs_options{
  126. unsigned int rock:1;
  127. unsigned int joliet:1;
  128. unsigned int cruft:1;
  129. unsigned int hide:1;
  130. unsigned int showassoc:1;
  131. unsigned int nocompress:1;
  132. unsigned int overriderockperm:1;
  133. unsigned int uid_set:1;
  134. unsigned int gid_set:1;
  135. unsigned char map;
  136. unsigned char check;
  137. unsigned int blocksize;
  138. umode_t fmode;
  139. umode_t dmode;
  140. kgid_t gid;
  141. kuid_t uid;
  142. char *iocharset;
  143. /* LVE */
  144. s32 session;
  145. s32 sbsector;
  146. };
  147. /*
  148. * Compute the hash for the isofs name corresponding to the dentry.
  149. */
  150. static int
  151. isofs_hashi_common(const struct dentry *dentry, struct qstr *qstr, int ms)
  152. {
  153. const char *name;
  154. int len;
  155. char c;
  156. unsigned long hash;
  157. len = qstr->len;
  158. name = qstr->name;
  159. if (ms) {
  160. while (len && name[len-1] == '.')
  161. len--;
  162. }
  163. hash = init_name_hash(dentry);
  164. while (len--) {
  165. c = tolower(*name++);
  166. hash = partial_name_hash(c, hash);
  167. }
  168. qstr->hash = end_name_hash(hash);
  169. return 0;
  170. }
  171. /*
  172. * Compare of two isofs names.
  173. */
  174. static int isofs_dentry_cmp_common(
  175. unsigned int len, const char *str,
  176. const struct qstr *name, int ms, int ci)
  177. {
  178. int alen, blen;
  179. /* A filename cannot end in '.' or we treat it like it has none */
  180. alen = name->len;
  181. blen = len;
  182. if (ms) {
  183. while (alen && name->name[alen-1] == '.')
  184. alen--;
  185. while (blen && str[blen-1] == '.')
  186. blen--;
  187. }
  188. if (alen == blen) {
  189. if (ci) {
  190. if (strncasecmp(name->name, str, alen) == 0)
  191. return 0;
  192. } else {
  193. if (strncmp(name->name, str, alen) == 0)
  194. return 0;
  195. }
  196. }
  197. return 1;
  198. }
  199. static int
  200. isofs_hashi(const struct dentry *dentry, struct qstr *qstr)
  201. {
  202. return isofs_hashi_common(dentry, qstr, 0);
  203. }
  204. static int
  205. isofs_dentry_cmpi(const struct dentry *dentry,
  206. unsigned int len, const char *str, const struct qstr *name)
  207. {
  208. return isofs_dentry_cmp_common(len, str, name, 0, 1);
  209. }
  210. #ifdef CONFIG_JOLIET
  211. /*
  212. * Compute the hash for the isofs name corresponding to the dentry.
  213. */
  214. static int
  215. isofs_hash_common(const struct dentry *dentry, struct qstr *qstr, int ms)
  216. {
  217. const char *name;
  218. int len;
  219. len = qstr->len;
  220. name = qstr->name;
  221. if (ms) {
  222. while (len && name[len-1] == '.')
  223. len--;
  224. }
  225. qstr->hash = full_name_hash(dentry, name, len);
  226. return 0;
  227. }
  228. static int
  229. isofs_hash_ms(const struct dentry *dentry, struct qstr *qstr)
  230. {
  231. return isofs_hash_common(dentry, qstr, 1);
  232. }
  233. static int
  234. isofs_hashi_ms(const struct dentry *dentry, struct qstr *qstr)
  235. {
  236. return isofs_hashi_common(dentry, qstr, 1);
  237. }
  238. static int
  239. isofs_dentry_cmp_ms(const struct dentry *dentry,
  240. unsigned int len, const char *str, const struct qstr *name)
  241. {
  242. return isofs_dentry_cmp_common(len, str, name, 1, 0);
  243. }
  244. static int
  245. isofs_dentry_cmpi_ms(const struct dentry *dentry,
  246. unsigned int len, const char *str, const struct qstr *name)
  247. {
  248. return isofs_dentry_cmp_common(len, str, name, 1, 1);
  249. }
  250. #endif
  251. enum {
  252. Opt_block, Opt_check, Opt_cruft, Opt_gid, Opt_ignore, Opt_iocharset,
  253. Opt_map, Opt_mode, Opt_nojoliet, Opt_norock, Opt_sb, Opt_session,
  254. Opt_uid, Opt_unhide, Opt_utf8, Opt_err, Opt_nocompress, Opt_hide,
  255. Opt_showassoc, Opt_dmode, Opt_overriderockperm,
  256. };
  257. static const struct constant_table isofs_param_map[] = {
  258. {"acorn", 'a'},
  259. {"a", 'a'},
  260. {"normal", 'n'},
  261. {"n", 'n'},
  262. {"off", 'o'},
  263. {"o", 'o'},
  264. {}
  265. };
  266. static const struct constant_table isofs_param_check[] = {
  267. {"relaxed", 'r'},
  268. {"r", 'r'},
  269. {"strict", 's'},
  270. {"s", 's'},
  271. {}
  272. };
  273. static const struct fs_parameter_spec isofs_param_spec[] = {
  274. fsparam_flag ("norock", Opt_norock),
  275. fsparam_flag ("nojoliet", Opt_nojoliet),
  276. fsparam_flag ("unhide", Opt_unhide),
  277. fsparam_flag ("hide", Opt_hide),
  278. fsparam_flag ("showassoc", Opt_showassoc),
  279. fsparam_flag ("cruft", Opt_cruft),
  280. fsparam_flag ("utf8", Opt_utf8),
  281. fsparam_string ("iocharset", Opt_iocharset),
  282. fsparam_enum ("map", Opt_map, isofs_param_map),
  283. fsparam_u32 ("session", Opt_session),
  284. fsparam_u32 ("sbsector", Opt_sb),
  285. fsparam_enum ("check", Opt_check, isofs_param_check),
  286. fsparam_uid ("uid", Opt_uid),
  287. fsparam_gid ("gid", Opt_gid),
  288. /* Note: mode/dmode historically accepted %u not strictly %o */
  289. fsparam_u32 ("mode", Opt_mode),
  290. fsparam_u32 ("dmode", Opt_dmode),
  291. fsparam_flag ("overriderockperm", Opt_overriderockperm),
  292. fsparam_u32 ("block", Opt_block),
  293. fsparam_string ("conv", Opt_ignore),
  294. fsparam_flag ("nocompress", Opt_nocompress),
  295. {}
  296. };
  297. static int isofs_parse_param(struct fs_context *fc,
  298. struct fs_parameter *param)
  299. {
  300. struct isofs_options *popt = fc->fs_private;
  301. struct fs_parse_result result;
  302. int opt;
  303. unsigned int n;
  304. /* There are no remountable options */
  305. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE)
  306. return 0;
  307. opt = fs_parse(fc, isofs_param_spec, param, &result);
  308. if (opt < 0)
  309. return opt;
  310. switch (opt) {
  311. case Opt_norock:
  312. popt->rock = 0;
  313. break;
  314. case Opt_nojoliet:
  315. popt->joliet = 0;
  316. break;
  317. case Opt_hide:
  318. popt->hide = 1;
  319. break;
  320. case Opt_unhide:
  321. case Opt_showassoc:
  322. popt->showassoc = 1;
  323. break;
  324. case Opt_cruft:
  325. popt->cruft = 1;
  326. break;
  327. #ifdef CONFIG_JOLIET
  328. case Opt_utf8:
  329. kfree(popt->iocharset);
  330. popt->iocharset = kstrdup("utf8", GFP_KERNEL);
  331. if (!popt->iocharset)
  332. return -ENOMEM;
  333. break;
  334. case Opt_iocharset:
  335. kfree(popt->iocharset);
  336. popt->iocharset = kstrdup(param->string, GFP_KERNEL);
  337. if (!popt->iocharset)
  338. return -ENOMEM;
  339. break;
  340. #endif
  341. case Opt_map:
  342. popt->map = result.uint_32;
  343. break;
  344. case Opt_session:
  345. n = result.uint_32;
  346. /*
  347. * Track numbers are supposed to be in range 1-99, the
  348. * mount option starts indexing at 0.
  349. */
  350. if (n >= 99)
  351. return -EINVAL;
  352. popt->session = n + 1;
  353. break;
  354. case Opt_sb:
  355. popt->sbsector = result.uint_32;
  356. break;
  357. case Opt_check:
  358. popt->check = result.uint_32;
  359. break;
  360. case Opt_ignore:
  361. break;
  362. case Opt_uid:
  363. popt->uid = result.uid;
  364. popt->uid_set = 1;
  365. break;
  366. case Opt_gid:
  367. popt->gid = result.gid;
  368. popt->gid_set = 1;
  369. break;
  370. case Opt_mode:
  371. popt->fmode = result.uint_32;
  372. break;
  373. case Opt_dmode:
  374. popt->dmode = result.uint_32;
  375. break;
  376. case Opt_overriderockperm:
  377. popt->overriderockperm = 1;
  378. break;
  379. case Opt_block:
  380. n = result.uint_32;
  381. if (n != 512 && n != 1024 && n != 2048)
  382. return -EINVAL;
  383. popt->blocksize = n;
  384. break;
  385. case Opt_nocompress:
  386. popt->nocompress = 1;
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. /*
  394. * Display the mount options in /proc/mounts.
  395. */
  396. static int isofs_show_options(struct seq_file *m, struct dentry *root)
  397. {
  398. struct isofs_sb_info *sbi = ISOFS_SB(root->d_sb);
  399. if (!sbi->s_rock) seq_puts(m, ",norock");
  400. else if (!sbi->s_joliet_level) seq_puts(m, ",nojoliet");
  401. if (sbi->s_cruft) seq_puts(m, ",cruft");
  402. if (sbi->s_hide) seq_puts(m, ",hide");
  403. if (sbi->s_nocompress) seq_puts(m, ",nocompress");
  404. if (sbi->s_overriderockperm) seq_puts(m, ",overriderockperm");
  405. if (sbi->s_showassoc) seq_puts(m, ",showassoc");
  406. if (sbi->s_check) seq_printf(m, ",check=%c", sbi->s_check);
  407. if (sbi->s_mapping) seq_printf(m, ",map=%c", sbi->s_mapping);
  408. if (sbi->s_session != 255) seq_printf(m, ",session=%u", sbi->s_session - 1);
  409. if (sbi->s_sbsector != -1) seq_printf(m, ",sbsector=%u", sbi->s_sbsector);
  410. if (root->d_sb->s_blocksize != 1024)
  411. seq_printf(m, ",blocksize=%lu", root->d_sb->s_blocksize);
  412. if (sbi->s_uid_set)
  413. seq_printf(m, ",uid=%u",
  414. from_kuid_munged(&init_user_ns, sbi->s_uid));
  415. if (sbi->s_gid_set)
  416. seq_printf(m, ",gid=%u",
  417. from_kgid_munged(&init_user_ns, sbi->s_gid));
  418. if (sbi->s_dmode != ISOFS_INVALID_MODE)
  419. seq_printf(m, ",dmode=%o", sbi->s_dmode);
  420. if (sbi->s_fmode != ISOFS_INVALID_MODE)
  421. seq_printf(m, ",fmode=%o", sbi->s_fmode);
  422. #ifdef CONFIG_JOLIET
  423. if (sbi->s_nls_iocharset)
  424. seq_printf(m, ",iocharset=%s", sbi->s_nls_iocharset->charset);
  425. else
  426. seq_puts(m, ",iocharset=utf8");
  427. #endif
  428. return 0;
  429. }
  430. /*
  431. * look if the driver can tell the multi session redirection value
  432. *
  433. * don't change this if you don't know what you do, please!
  434. * Multisession is legal only with XA disks.
  435. * A non-XA disk with more than one volume descriptor may do it right, but
  436. * usually is written in a nowhere standardized "multi-partition" manner.
  437. * Multisession uses absolute addressing (solely the first frame of the whole
  438. * track is #0), multi-partition uses relative addressing (each first frame of
  439. * each track is #0), and a track is not a session.
  440. *
  441. * A broken CDwriter software or drive firmware does not set new standards,
  442. * at least not if conflicting with the existing ones.
  443. *
  444. * emoenke@gwdg.de
  445. */
  446. #define WE_OBEY_THE_WRITTEN_STANDARDS 1
  447. static unsigned int isofs_get_last_session(struct super_block *sb, s32 session)
  448. {
  449. struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
  450. unsigned int vol_desc_start = 0;
  451. if (session > 0) {
  452. struct cdrom_tocentry te;
  453. if (!cdi)
  454. return 0;
  455. te.cdte_track = session;
  456. te.cdte_format = CDROM_LBA;
  457. if (cdrom_read_tocentry(cdi, &te) == 0) {
  458. printk(KERN_DEBUG "ISOFS: Session %d start %d type %d\n",
  459. session, te.cdte_addr.lba,
  460. te.cdte_ctrl & CDROM_DATA_TRACK);
  461. if ((te.cdte_ctrl & CDROM_DATA_TRACK) == 4)
  462. return te.cdte_addr.lba;
  463. }
  464. printk(KERN_ERR "ISOFS: Invalid session number or type of track\n");
  465. }
  466. if (cdi) {
  467. struct cdrom_multisession ms_info;
  468. ms_info.addr_format = CDROM_LBA;
  469. if (cdrom_multisession(cdi, &ms_info) == 0) {
  470. #if WE_OBEY_THE_WRITTEN_STANDARDS
  471. /* necessary for a valid ms_info.addr */
  472. if (ms_info.xa_flag)
  473. #endif
  474. vol_desc_start = ms_info.addr.lba;
  475. }
  476. }
  477. return vol_desc_start;
  478. }
  479. /*
  480. * Check if root directory is empty (has less than 3 files).
  481. *
  482. * Used to detect broken CDs where ISO root directory is empty but Joliet root
  483. * directory is OK. If such CD has Rock Ridge extensions, they will be disabled
  484. * (and Joliet used instead) or else no files would be visible.
  485. */
  486. static bool rootdir_empty(struct super_block *sb, unsigned long block)
  487. {
  488. int offset = 0, files = 0, de_len;
  489. struct iso_directory_record *de;
  490. struct buffer_head *bh;
  491. bh = sb_bread(sb, block);
  492. if (!bh)
  493. return true;
  494. while (files < 3) {
  495. de = (struct iso_directory_record *) (bh->b_data + offset);
  496. de_len = *(unsigned char *) de;
  497. if (de_len == 0)
  498. break;
  499. files++;
  500. offset += de_len;
  501. }
  502. brelse(bh);
  503. return files < 3;
  504. }
  505. /*
  506. * Initialize the superblock and read the root inode.
  507. */
  508. static int isofs_fill_super(struct super_block *s, struct fs_context *fc)
  509. {
  510. struct buffer_head *bh = NULL, *pri_bh = NULL;
  511. struct hs_primary_descriptor *h_pri = NULL;
  512. struct iso_primary_descriptor *pri = NULL;
  513. struct iso_supplementary_descriptor *sec = NULL;
  514. struct iso_directory_record *rootp;
  515. struct inode *inode;
  516. struct isofs_options *opt = fc->fs_private;
  517. struct isofs_sb_info *sbi;
  518. unsigned long first_data_zone;
  519. int joliet_level = 0;
  520. int iso_blknum, block;
  521. int orig_zonesize;
  522. int table, error = -EINVAL;
  523. unsigned int vol_desc_start;
  524. int silent = fc->sb_flags & SB_SILENT;
  525. sbi = kzalloc_obj(*sbi);
  526. if (!sbi)
  527. return -ENOMEM;
  528. s->s_fs_info = sbi;
  529. /*
  530. * First of all, get the hardware blocksize for this device.
  531. * If we don't know what it is, or the hardware blocksize is
  532. * larger than the blocksize the user specified, then use
  533. * that value.
  534. */
  535. /*
  536. * What if bugger tells us to go beyond page size?
  537. */
  538. if (bdev_logical_block_size(s->s_bdev) > 2048) {
  539. printk(KERN_WARNING
  540. "ISOFS: unsupported/invalid hardware sector size %d\n",
  541. bdev_logical_block_size(s->s_bdev));
  542. goto out_freesbi;
  543. }
  544. opt->blocksize = sb_min_blocksize(s, opt->blocksize);
  545. if (!opt->blocksize) {
  546. printk(KERN_ERR
  547. "ISOFS: unable to set blocksize\n");
  548. goto out_freesbi;
  549. }
  550. sbi->s_high_sierra = 0; /* default is iso9660 */
  551. sbi->s_session = opt->session;
  552. sbi->s_sbsector = opt->sbsector;
  553. vol_desc_start = (opt->sbsector != -1) ?
  554. opt->sbsector : isofs_get_last_session(s, opt->session);
  555. for (iso_blknum = vol_desc_start+16;
  556. iso_blknum < vol_desc_start+100; iso_blknum++) {
  557. struct hs_volume_descriptor *hdp;
  558. struct iso_volume_descriptor *vdp;
  559. block = iso_blknum << (ISOFS_BLOCK_BITS - s->s_blocksize_bits);
  560. if (!(bh = sb_bread(s, block)))
  561. goto out_no_read;
  562. vdp = (struct iso_volume_descriptor *)bh->b_data;
  563. hdp = (struct hs_volume_descriptor *)bh->b_data;
  564. /*
  565. * Due to the overlapping physical location of the descriptors,
  566. * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
  567. * proper identification in this case, we first check for ISO.
  568. */
  569. if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
  570. if (isonum_711(vdp->type) == ISO_VD_END)
  571. break;
  572. if (isonum_711(vdp->type) == ISO_VD_PRIMARY) {
  573. if (!pri) {
  574. pri = (struct iso_primary_descriptor *)vdp;
  575. /* Save the buffer in case we need it ... */
  576. pri_bh = bh;
  577. bh = NULL;
  578. }
  579. }
  580. #ifdef CONFIG_JOLIET
  581. else if (isonum_711(vdp->type) == ISO_VD_SUPPLEMENTARY) {
  582. sec = (struct iso_supplementary_descriptor *)vdp;
  583. if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
  584. if (opt->joliet) {
  585. if (sec->escape[2] == 0x40)
  586. joliet_level = 1;
  587. else if (sec->escape[2] == 0x43)
  588. joliet_level = 2;
  589. else if (sec->escape[2] == 0x45)
  590. joliet_level = 3;
  591. printk(KERN_DEBUG "ISO 9660 Extensions: "
  592. "Microsoft Joliet Level %d\n",
  593. joliet_level);
  594. }
  595. goto root_found;
  596. } else {
  597. /* Unknown supplementary volume descriptor */
  598. sec = NULL;
  599. }
  600. }
  601. #endif
  602. } else {
  603. if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
  604. if (isonum_711(hdp->type) != ISO_VD_PRIMARY)
  605. goto out_freebh;
  606. sbi->s_high_sierra = 1;
  607. opt->rock = 0;
  608. h_pri = (struct hs_primary_descriptor *)vdp;
  609. goto root_found;
  610. }
  611. }
  612. /* Just skip any volume descriptors we don't recognize */
  613. brelse(bh);
  614. bh = NULL;
  615. }
  616. /*
  617. * If we fall through, either no volume descriptor was found,
  618. * or else we passed a primary descriptor looking for others.
  619. */
  620. if (!pri)
  621. goto out_unknown_format;
  622. brelse(bh);
  623. bh = pri_bh;
  624. pri_bh = NULL;
  625. root_found:
  626. /* We don't support read-write mounts */
  627. if (!sb_rdonly(s)) {
  628. error = -EACCES;
  629. goto out_freebh;
  630. }
  631. if (joliet_level && (!pri || !opt->rock)) {
  632. /* This is the case of Joliet with the norock mount flag.
  633. * A disc with both Joliet and Rock Ridge is handled later
  634. */
  635. pri = (struct iso_primary_descriptor *) sec;
  636. }
  637. if(sbi->s_high_sierra){
  638. rootp = (struct iso_directory_record *) h_pri->root_directory_record;
  639. sbi->s_nzones = isonum_733(h_pri->volume_space_size);
  640. sbi->s_log_zone_size = isonum_723(h_pri->logical_block_size);
  641. sbi->s_max_size = isonum_733(h_pri->volume_space_size);
  642. } else {
  643. if (!pri)
  644. goto out_freebh;
  645. rootp = (struct iso_directory_record *) pri->root_directory_record;
  646. sbi->s_nzones = isonum_733(pri->volume_space_size);
  647. sbi->s_log_zone_size = isonum_723(pri->logical_block_size);
  648. sbi->s_max_size = isonum_733(pri->volume_space_size);
  649. }
  650. sbi->s_ninodes = 0; /* No way to figure this out easily */
  651. orig_zonesize = sbi->s_log_zone_size;
  652. /*
  653. * If the zone size is smaller than the hardware sector size,
  654. * this is a fatal error. This would occur if the disc drive
  655. * had sectors that were 2048 bytes, but the filesystem had
  656. * blocks that were 512 bytes (which should only very rarely
  657. * happen.)
  658. */
  659. if (orig_zonesize < opt->blocksize)
  660. goto out_bad_size;
  661. /* RDE: convert log zone size to bit shift */
  662. switch (sbi->s_log_zone_size) {
  663. case 512: sbi->s_log_zone_size = 9; break;
  664. case 1024: sbi->s_log_zone_size = 10; break;
  665. case 2048: sbi->s_log_zone_size = 11; break;
  666. default:
  667. goto out_bad_zone_size;
  668. }
  669. s->s_magic = ISOFS_SUPER_MAGIC;
  670. /*
  671. * With multi-extent files, file size is only limited by the maximum
  672. * size of a file system, which is 8 TB.
  673. */
  674. s->s_maxbytes = 0x80000000000LL;
  675. /* ECMA-119 timestamp from 1900/1/1 with tz offset */
  676. s->s_time_min = mktime64(1900, 1, 1, 0, 0, 0) - MAX_TZ_OFFSET;
  677. s->s_time_max = mktime64(U8_MAX+1900, 12, 31, 23, 59, 59) + MAX_TZ_OFFSET;
  678. /* Set this for reference. Its not currently used except on write
  679. which we don't have .. */
  680. first_data_zone = isonum_733(rootp->extent) +
  681. isonum_711(rootp->ext_attr_length);
  682. sbi->s_firstdatazone = first_data_zone;
  683. #ifndef BEQUIET
  684. printk(KERN_DEBUG "ISOFS: Max size:%ld Log zone size:%ld\n",
  685. sbi->s_max_size, 1UL << sbi->s_log_zone_size);
  686. printk(KERN_DEBUG "ISOFS: First datazone:%ld\n", sbi->s_firstdatazone);
  687. if(sbi->s_high_sierra)
  688. printk(KERN_DEBUG "ISOFS: Disc in High Sierra format.\n");
  689. #endif
  690. /*
  691. * If the Joliet level is set, we _may_ decide to use the
  692. * secondary descriptor, but can't be sure until after we
  693. * read the root inode. But before reading the root inode
  694. * we may need to change the device blocksize, and would
  695. * rather release the old buffer first. So, we cache the
  696. * first_data_zone value from the secondary descriptor.
  697. */
  698. if (joliet_level) {
  699. pri = (struct iso_primary_descriptor *) sec;
  700. rootp = (struct iso_directory_record *)
  701. pri->root_directory_record;
  702. first_data_zone = isonum_733(rootp->extent) +
  703. isonum_711(rootp->ext_attr_length);
  704. }
  705. /*
  706. * We're all done using the volume descriptor, and may need
  707. * to change the device blocksize, so release the buffer now.
  708. */
  709. brelse(pri_bh);
  710. brelse(bh);
  711. /*
  712. * Force the blocksize to 512 for 512 byte sectors. The file
  713. * read primitives really get it wrong in a bad way if we don't
  714. * do this.
  715. *
  716. * Note - we should never be setting the blocksize to something
  717. * less than the hardware sector size for the device. If we
  718. * do, we would end up having to read larger buffers and split
  719. * out portions to satisfy requests.
  720. *
  721. * Note2- the idea here is that we want to deal with the optimal
  722. * zonesize in the filesystem. If we have it set to something less,
  723. * then we have horrible problems with trying to piece together
  724. * bits of adjacent blocks in order to properly read directory
  725. * entries. By forcing the blocksize in this way, we ensure
  726. * that we will never be required to do this.
  727. */
  728. sb_set_blocksize(s, orig_zonesize);
  729. sbi->s_nls_iocharset = NULL;
  730. #ifdef CONFIG_JOLIET
  731. if (joliet_level) {
  732. char *p = opt->iocharset ? opt->iocharset : CONFIG_NLS_DEFAULT;
  733. if (strcmp(p, "utf8") != 0) {
  734. sbi->s_nls_iocharset = opt->iocharset ?
  735. load_nls(opt->iocharset) : load_nls_default();
  736. if (!sbi->s_nls_iocharset)
  737. goto out_freesbi;
  738. }
  739. }
  740. #endif
  741. s->s_op = &isofs_sops;
  742. s->s_export_op = &isofs_export_ops;
  743. sbi->s_mapping = opt->map;
  744. sbi->s_rock = (opt->rock ? 2 : 0);
  745. sbi->s_rock_offset = -1; /* initial offset, will guess until SP is found*/
  746. sbi->s_cruft = opt->cruft;
  747. sbi->s_hide = opt->hide;
  748. sbi->s_showassoc = opt->showassoc;
  749. sbi->s_uid = opt->uid;
  750. sbi->s_gid = opt->gid;
  751. sbi->s_uid_set = opt->uid_set;
  752. sbi->s_gid_set = opt->gid_set;
  753. sbi->s_nocompress = opt->nocompress;
  754. sbi->s_overriderockperm = opt->overriderockperm;
  755. /*
  756. * It would be incredibly stupid to allow people to mark every file
  757. * on the disk as suid, so we merely allow them to set the default
  758. * permissions.
  759. */
  760. if (opt->fmode != ISOFS_INVALID_MODE)
  761. sbi->s_fmode = opt->fmode & 0777;
  762. else
  763. sbi->s_fmode = ISOFS_INVALID_MODE;
  764. if (opt->dmode != ISOFS_INVALID_MODE)
  765. sbi->s_dmode = opt->dmode & 0777;
  766. else
  767. sbi->s_dmode = ISOFS_INVALID_MODE;
  768. /*
  769. * Read the root inode, which _may_ result in changing
  770. * the s_rock flag. Once we have the final s_rock value,
  771. * we then decide whether to use the Joliet descriptor.
  772. */
  773. inode = isofs_iget(s, sbi->s_firstdatazone, 0);
  774. /*
  775. * Fix for broken CDs with a corrupt root inode but a correct Joliet
  776. * root directory.
  777. */
  778. if (IS_ERR(inode)) {
  779. if (joliet_level && sbi->s_firstdatazone != first_data_zone) {
  780. printk(KERN_NOTICE
  781. "ISOFS: root inode is unusable. "
  782. "Disabling Rock Ridge and switching to Joliet.");
  783. sbi->s_rock = 0;
  784. inode = NULL;
  785. } else {
  786. goto out_no_root;
  787. }
  788. }
  789. /*
  790. * Fix for broken CDs with Rock Ridge and empty ISO root directory but
  791. * correct Joliet root directory.
  792. */
  793. if (sbi->s_rock == 1 && joliet_level &&
  794. rootdir_empty(s, sbi->s_firstdatazone)) {
  795. printk(KERN_NOTICE
  796. "ISOFS: primary root directory is empty. "
  797. "Disabling Rock Ridge and switching to Joliet.");
  798. sbi->s_rock = 0;
  799. }
  800. /*
  801. * If this disk has both Rock Ridge and Joliet on it, then we
  802. * want to use Rock Ridge by default. This can be overridden
  803. * by using the norock mount option. There is still one other
  804. * possibility that is not taken into account: a Rock Ridge
  805. * CD with Unicode names. Until someone sees such a beast, it
  806. * will not be supported.
  807. */
  808. if (sbi->s_rock == 1) {
  809. joliet_level = 0;
  810. } else if (joliet_level) {
  811. sbi->s_rock = 0;
  812. if (sbi->s_firstdatazone != first_data_zone) {
  813. sbi->s_firstdatazone = first_data_zone;
  814. printk(KERN_DEBUG
  815. "ISOFS: changing to secondary root\n");
  816. iput(inode);
  817. inode = isofs_iget(s, sbi->s_firstdatazone, 0);
  818. if (IS_ERR(inode))
  819. goto out_no_root;
  820. }
  821. }
  822. if (opt->check == 'u') {
  823. /* Only Joliet is case insensitive by default */
  824. if (joliet_level)
  825. opt->check = 'r';
  826. else
  827. opt->check = 's';
  828. }
  829. sbi->s_joliet_level = joliet_level;
  830. /* Make sure the root inode is a directory */
  831. if (!S_ISDIR(inode->i_mode)) {
  832. printk(KERN_WARNING
  833. "isofs_fill_super: root inode is not a directory. "
  834. "Corrupted media?\n");
  835. goto out_iput;
  836. }
  837. table = 0;
  838. if (joliet_level)
  839. table += 2;
  840. if (opt->check == 'r')
  841. table++;
  842. sbi->s_check = opt->check;
  843. if (table)
  844. set_default_d_op(s, &isofs_dentry_ops[table - 1]);
  845. /* get the root dentry */
  846. s->s_root = d_make_root(inode);
  847. if (!(s->s_root)) {
  848. error = -ENOMEM;
  849. goto out_no_inode;
  850. }
  851. return 0;
  852. /*
  853. * Display error messages and free resources.
  854. */
  855. out_iput:
  856. iput(inode);
  857. goto out_no_inode;
  858. out_no_root:
  859. error = PTR_ERR(inode);
  860. if (error != -ENOMEM)
  861. printk(KERN_WARNING "%s: get root inode failed\n", __func__);
  862. out_no_inode:
  863. #ifdef CONFIG_JOLIET
  864. unload_nls(sbi->s_nls_iocharset);
  865. #endif
  866. goto out_freesbi;
  867. out_no_read:
  868. printk(KERN_WARNING "%s: bread failed, dev=%s, iso_blknum=%d, block=%d\n",
  869. __func__, s->s_id, iso_blknum, block);
  870. goto out_freebh;
  871. out_bad_zone_size:
  872. printk(KERN_WARNING "ISOFS: Bad logical zone size %ld\n",
  873. sbi->s_log_zone_size);
  874. goto out_freebh;
  875. out_bad_size:
  876. printk(KERN_WARNING "ISOFS: Logical zone size(%d) < hardware blocksize(%u)\n",
  877. orig_zonesize, opt->blocksize);
  878. goto out_freebh;
  879. out_unknown_format:
  880. if (!silent)
  881. printk(KERN_WARNING "ISOFS: Unable to identify CD-ROM format.\n");
  882. out_freebh:
  883. brelse(bh);
  884. brelse(pri_bh);
  885. out_freesbi:
  886. kfree(sbi);
  887. s->s_fs_info = NULL;
  888. return error;
  889. }
  890. static int isofs_statfs (struct dentry *dentry, struct kstatfs *buf)
  891. {
  892. struct super_block *sb = dentry->d_sb;
  893. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  894. buf->f_type = ISOFS_SUPER_MAGIC;
  895. buf->f_bsize = sb->s_blocksize;
  896. buf->f_blocks = (ISOFS_SB(sb)->s_nzones
  897. << (ISOFS_SB(sb)->s_log_zone_size - sb->s_blocksize_bits));
  898. buf->f_bfree = 0;
  899. buf->f_bavail = 0;
  900. buf->f_files = ISOFS_SB(sb)->s_ninodes;
  901. buf->f_ffree = 0;
  902. buf->f_fsid = u64_to_fsid(id);
  903. buf->f_namelen = NAME_MAX;
  904. return 0;
  905. }
  906. /*
  907. * Get a set of blocks; filling in buffer_heads if already allocated
  908. * or getblk() if they are not. Returns the number of blocks inserted
  909. * (-ve == error.)
  910. */
  911. int isofs_get_blocks(struct inode *inode, sector_t iblock,
  912. struct buffer_head **bh, unsigned long nblocks)
  913. {
  914. unsigned long b_off = iblock;
  915. unsigned offset, sect_size;
  916. unsigned int firstext;
  917. unsigned long nextblk, nextoff;
  918. int section, rv, error;
  919. struct iso_inode_info *ei = ISOFS_I(inode);
  920. error = -EIO;
  921. rv = 0;
  922. if (iblock != b_off) {
  923. printk(KERN_DEBUG "%s: block number too large\n", __func__);
  924. goto abort;
  925. }
  926. offset = 0;
  927. firstext = ei->i_first_extent;
  928. sect_size = ei->i_section_size >> ISOFS_BUFFER_BITS(inode);
  929. nextblk = ei->i_next_section_block;
  930. nextoff = ei->i_next_section_offset;
  931. section = 0;
  932. while (nblocks) {
  933. /* If we are *way* beyond the end of the file, print a message.
  934. * Access beyond the end of the file up to the next page boundary
  935. * is normal, however because of the way the page cache works.
  936. * In this case, we just return 0 so that we can properly fill
  937. * the page with useless information without generating any
  938. * I/O errors.
  939. */
  940. if (b_off > ((inode->i_size + PAGE_SIZE - 1) >> ISOFS_BUFFER_BITS(inode))) {
  941. printk(KERN_DEBUG "%s: block >= EOF (%lu, %llu)\n",
  942. __func__, b_off,
  943. (unsigned long long)inode->i_size);
  944. goto abort;
  945. }
  946. /* On the last section, nextblk == 0, section size is likely to
  947. * exceed sect_size by a partial block, and access beyond the
  948. * end of the file will reach beyond the section size, too.
  949. */
  950. while (nextblk && (b_off >= (offset + sect_size))) {
  951. struct inode *ninode;
  952. offset += sect_size;
  953. ninode = isofs_iget(inode->i_sb, nextblk, nextoff);
  954. if (IS_ERR(ninode)) {
  955. error = PTR_ERR(ninode);
  956. goto abort;
  957. }
  958. firstext = ISOFS_I(ninode)->i_first_extent;
  959. sect_size = ISOFS_I(ninode)->i_section_size >> ISOFS_BUFFER_BITS(ninode);
  960. nextblk = ISOFS_I(ninode)->i_next_section_block;
  961. nextoff = ISOFS_I(ninode)->i_next_section_offset;
  962. iput(ninode);
  963. if (++section > 100) {
  964. printk(KERN_DEBUG "%s: More than 100 file sections ?!?"
  965. " aborting...\n", __func__);
  966. printk(KERN_DEBUG "%s: block=%lu firstext=%u sect_size=%u "
  967. "nextblk=%lu nextoff=%lu\n", __func__,
  968. b_off, firstext, (unsigned) sect_size,
  969. nextblk, nextoff);
  970. goto abort;
  971. }
  972. }
  973. if (*bh) {
  974. map_bh(*bh, inode->i_sb, firstext + b_off - offset);
  975. } else {
  976. *bh = sb_getblk(inode->i_sb, firstext+b_off-offset);
  977. if (!*bh)
  978. goto abort;
  979. }
  980. bh++; /* Next buffer head */
  981. b_off++; /* Next buffer offset */
  982. nblocks--;
  983. rv++;
  984. }
  985. error = 0;
  986. abort:
  987. return rv != 0 ? rv : error;
  988. }
  989. /*
  990. * Used by the standard interfaces.
  991. */
  992. static int isofs_get_block(struct inode *inode, sector_t iblock,
  993. struct buffer_head *bh_result, int create)
  994. {
  995. int ret;
  996. if (create) {
  997. printk(KERN_DEBUG "%s: Kernel tries to allocate a block\n", __func__);
  998. return -EROFS;
  999. }
  1000. ret = isofs_get_blocks(inode, iblock, &bh_result, 1);
  1001. return ret < 0 ? ret : 0;
  1002. }
  1003. static int isofs_bmap(struct inode *inode, sector_t block)
  1004. {
  1005. struct buffer_head dummy;
  1006. int error;
  1007. dummy.b_state = 0;
  1008. dummy.b_blocknr = -1000;
  1009. error = isofs_get_block(inode, block, &dummy, 0);
  1010. if (!error)
  1011. return dummy.b_blocknr;
  1012. return 0;
  1013. }
  1014. struct buffer_head *isofs_bread(struct inode *inode, sector_t block)
  1015. {
  1016. sector_t blknr = isofs_bmap(inode, block);
  1017. if (!blknr)
  1018. return NULL;
  1019. return sb_bread(inode->i_sb, blknr);
  1020. }
  1021. static int isofs_read_folio(struct file *file, struct folio *folio)
  1022. {
  1023. return mpage_read_folio(folio, isofs_get_block);
  1024. }
  1025. static void isofs_readahead(struct readahead_control *rac)
  1026. {
  1027. mpage_readahead(rac, isofs_get_block);
  1028. }
  1029. static sector_t _isofs_bmap(struct address_space *mapping, sector_t block)
  1030. {
  1031. return generic_block_bmap(mapping,block,isofs_get_block);
  1032. }
  1033. static const struct address_space_operations isofs_aops = {
  1034. .read_folio = isofs_read_folio,
  1035. .readahead = isofs_readahead,
  1036. .bmap = _isofs_bmap
  1037. };
  1038. static int isofs_read_level3_size(struct inode *inode)
  1039. {
  1040. unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  1041. int high_sierra = ISOFS_SB(inode->i_sb)->s_high_sierra;
  1042. struct buffer_head *bh = NULL;
  1043. unsigned long block, offset, block_saved, offset_saved;
  1044. int i = 0;
  1045. int more_entries = 0;
  1046. struct iso_directory_record *tmpde = NULL;
  1047. struct iso_inode_info *ei = ISOFS_I(inode);
  1048. inode->i_size = 0;
  1049. /* The first 16 blocks are reserved as the System Area. Thus,
  1050. * no inodes can appear in block 0. We use this to flag that
  1051. * this is the last section. */
  1052. ei->i_next_section_block = 0;
  1053. ei->i_next_section_offset = 0;
  1054. block = ei->i_iget5_block;
  1055. offset = ei->i_iget5_offset;
  1056. do {
  1057. struct iso_directory_record *de;
  1058. unsigned int de_len;
  1059. if (!bh) {
  1060. bh = sb_bread(inode->i_sb, block);
  1061. if (!bh)
  1062. goto out_noread;
  1063. }
  1064. de = (struct iso_directory_record *) (bh->b_data + offset);
  1065. de_len = *(unsigned char *) de;
  1066. if (de_len == 0) {
  1067. brelse(bh);
  1068. bh = NULL;
  1069. ++block;
  1070. offset = 0;
  1071. continue;
  1072. }
  1073. block_saved = block;
  1074. offset_saved = offset;
  1075. offset += de_len;
  1076. /* Make sure we have a full directory entry */
  1077. if (offset >= bufsize) {
  1078. int slop = bufsize - offset + de_len;
  1079. if (!tmpde) {
  1080. tmpde = kmalloc(256, GFP_KERNEL);
  1081. if (!tmpde)
  1082. goto out_nomem;
  1083. }
  1084. memcpy(tmpde, de, slop);
  1085. offset &= bufsize - 1;
  1086. block++;
  1087. brelse(bh);
  1088. bh = NULL;
  1089. if (offset) {
  1090. bh = sb_bread(inode->i_sb, block);
  1091. if (!bh)
  1092. goto out_noread;
  1093. memcpy((void *)tmpde+slop, bh->b_data, offset);
  1094. }
  1095. de = tmpde;
  1096. }
  1097. inode->i_size += isonum_733(de->size);
  1098. if (i == 1) {
  1099. ei->i_next_section_block = block_saved;
  1100. ei->i_next_section_offset = offset_saved;
  1101. }
  1102. more_entries = de->flags[-high_sierra] & 0x80;
  1103. i++;
  1104. if (i > 100)
  1105. goto out_toomany;
  1106. } while (more_entries);
  1107. out:
  1108. kfree(tmpde);
  1109. brelse(bh);
  1110. return 0;
  1111. out_nomem:
  1112. brelse(bh);
  1113. return -ENOMEM;
  1114. out_noread:
  1115. printk(KERN_INFO "ISOFS: unable to read i-node block %lu\n", block);
  1116. kfree(tmpde);
  1117. return -EIO;
  1118. out_toomany:
  1119. printk(KERN_INFO "%s: More than 100 file sections ?!?, aborting...\n"
  1120. "isofs_read_level3_size: inode=%lu\n",
  1121. __func__, inode->i_ino);
  1122. goto out;
  1123. }
  1124. static int isofs_read_inode(struct inode *inode, int relocated)
  1125. {
  1126. struct super_block *sb = inode->i_sb;
  1127. struct isofs_sb_info *sbi = ISOFS_SB(sb);
  1128. unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  1129. unsigned long block;
  1130. int high_sierra = sbi->s_high_sierra;
  1131. struct buffer_head *bh;
  1132. struct iso_directory_record *de;
  1133. struct iso_directory_record *tmpde = NULL;
  1134. unsigned int de_len;
  1135. unsigned long offset;
  1136. struct iso_inode_info *ei = ISOFS_I(inode);
  1137. int ret = -EIO;
  1138. struct timespec64 ts;
  1139. block = ei->i_iget5_block;
  1140. bh = sb_bread(inode->i_sb, block);
  1141. if (!bh)
  1142. goto out_badread;
  1143. offset = ei->i_iget5_offset;
  1144. de = (struct iso_directory_record *) (bh->b_data + offset);
  1145. de_len = *(unsigned char *) de;
  1146. if (de_len < sizeof(struct iso_directory_record))
  1147. goto fail;
  1148. if (offset + de_len > bufsize) {
  1149. int frag1 = bufsize - offset;
  1150. tmpde = kmalloc(de_len, GFP_KERNEL);
  1151. if (!tmpde) {
  1152. ret = -ENOMEM;
  1153. goto fail;
  1154. }
  1155. memcpy(tmpde, bh->b_data + offset, frag1);
  1156. brelse(bh);
  1157. bh = sb_bread(inode->i_sb, ++block);
  1158. if (!bh)
  1159. goto out_badread;
  1160. memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
  1161. de = tmpde;
  1162. }
  1163. inode->i_ino = isofs_get_ino(ei->i_iget5_block,
  1164. ei->i_iget5_offset,
  1165. ISOFS_BUFFER_BITS(inode));
  1166. /* Assume it is a normal-format file unless told otherwise */
  1167. ei->i_file_format = isofs_file_normal;
  1168. if (de->flags[-high_sierra] & 2) {
  1169. if (sbi->s_dmode != ISOFS_INVALID_MODE)
  1170. inode->i_mode = S_IFDIR | sbi->s_dmode;
  1171. else
  1172. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1173. set_nlink(inode, 1); /*
  1174. * Set to 1. We know there are 2, but
  1175. * the find utility tries to optimize
  1176. * if it is 2, and it screws up. It is
  1177. * easier to give 1 which tells find to
  1178. * do it the hard way.
  1179. */
  1180. } else {
  1181. if (sbi->s_fmode != ISOFS_INVALID_MODE) {
  1182. inode->i_mode = S_IFREG | sbi->s_fmode;
  1183. } else {
  1184. /*
  1185. * Set default permissions: r-x for all. The disc
  1186. * could be shared with DOS machines so virtually
  1187. * anything could be a valid executable.
  1188. */
  1189. inode->i_mode = S_IFREG | S_IRUGO | S_IXUGO;
  1190. }
  1191. set_nlink(inode, 1);
  1192. }
  1193. inode->i_uid = sbi->s_uid;
  1194. inode->i_gid = sbi->s_gid;
  1195. inode->i_blocks = 0;
  1196. ei->i_format_parm[0] = 0;
  1197. ei->i_format_parm[1] = 0;
  1198. ei->i_format_parm[2] = 0;
  1199. ei->i_section_size = isonum_733(de->size);
  1200. if (de->flags[-high_sierra] & 0x80) {
  1201. ret = isofs_read_level3_size(inode);
  1202. if (ret < 0)
  1203. goto fail;
  1204. ret = -EIO;
  1205. } else {
  1206. ei->i_next_section_block = 0;
  1207. ei->i_next_section_offset = 0;
  1208. inode->i_size = isonum_733(de->size);
  1209. }
  1210. /*
  1211. * Some dipshit decided to store some other bit of information
  1212. * in the high byte of the file length. Truncate size in case
  1213. * this CDROM was mounted with the cruft option.
  1214. */
  1215. if (sbi->s_cruft)
  1216. inode->i_size &= 0x00ffffff;
  1217. if (de->interleave[0]) {
  1218. printk(KERN_DEBUG "ISOFS: Interleaved files not (yet) supported.\n");
  1219. inode->i_size = 0;
  1220. }
  1221. /* I have no idea what file_unit_size is used for, so
  1222. we will flag it for now */
  1223. if (de->file_unit_size[0] != 0) {
  1224. printk(KERN_DEBUG "ISOFS: File unit size != 0 for ISO file (%ld).\n",
  1225. inode->i_ino);
  1226. }
  1227. /* I have no idea what other flag bits are used for, so
  1228. we will flag it for now */
  1229. #ifdef DEBUG
  1230. if((de->flags[-high_sierra] & ~2)!= 0){
  1231. printk(KERN_DEBUG "ISOFS: Unusual flag settings for ISO file "
  1232. "(%ld %x).\n",
  1233. inode->i_ino, de->flags[-high_sierra]);
  1234. }
  1235. #endif
  1236. ts = iso_date(de->date, high_sierra ? ISO_DATE_HIGH_SIERRA : 0);
  1237. inode_set_ctime_to_ts(inode, ts);
  1238. inode_set_atime_to_ts(inode, ts);
  1239. inode_set_mtime_to_ts(inode, ts);
  1240. ei->i_first_extent = (isonum_733(de->extent) +
  1241. isonum_711(de->ext_attr_length));
  1242. /* Set the number of blocks for stat() - should be done before RR */
  1243. inode->i_blocks = (inode->i_size + 511) >> 9;
  1244. /*
  1245. * Now test for possible Rock Ridge extensions which will override
  1246. * some of these numbers in the inode structure.
  1247. */
  1248. if (!high_sierra) {
  1249. parse_rock_ridge_inode(de, inode, relocated);
  1250. /* if we want uid/gid set, override the rock ridge setting */
  1251. if (sbi->s_uid_set)
  1252. inode->i_uid = sbi->s_uid;
  1253. if (sbi->s_gid_set)
  1254. inode->i_gid = sbi->s_gid;
  1255. }
  1256. /* Now set final access rights if overriding rock ridge setting */
  1257. if (S_ISDIR(inode->i_mode) && sbi->s_overriderockperm &&
  1258. sbi->s_dmode != ISOFS_INVALID_MODE)
  1259. inode->i_mode = S_IFDIR | sbi->s_dmode;
  1260. if (S_ISREG(inode->i_mode) && sbi->s_overriderockperm &&
  1261. sbi->s_fmode != ISOFS_INVALID_MODE)
  1262. inode->i_mode = S_IFREG | sbi->s_fmode;
  1263. /* Install the inode operations vector */
  1264. if (S_ISREG(inode->i_mode)) {
  1265. inode->i_fop = &generic_ro_fops;
  1266. switch (ei->i_file_format) {
  1267. #ifdef CONFIG_ZISOFS
  1268. case isofs_file_compressed:
  1269. inode->i_data.a_ops = &zisofs_aops;
  1270. break;
  1271. #endif
  1272. default:
  1273. inode->i_data.a_ops = &isofs_aops;
  1274. break;
  1275. }
  1276. } else if (S_ISDIR(inode->i_mode)) {
  1277. inode->i_op = &isofs_dir_inode_operations;
  1278. inode->i_fop = &isofs_dir_operations;
  1279. } else if (S_ISLNK(inode->i_mode)) {
  1280. inode->i_op = &page_symlink_inode_operations;
  1281. inode_nohighmem(inode);
  1282. inode->i_data.a_ops = &isofs_symlink_aops;
  1283. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  1284. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1285. /* XXX - parse_rock_ridge_inode() had already set i_rdev. */
  1286. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1287. } else {
  1288. printk(KERN_DEBUG "ISOFS: Invalid file type 0%04o for inode %lu.\n",
  1289. inode->i_mode, inode->i_ino);
  1290. ret = -EIO;
  1291. goto fail;
  1292. }
  1293. ret = 0;
  1294. out:
  1295. kfree(tmpde);
  1296. brelse(bh);
  1297. return ret;
  1298. out_badread:
  1299. printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
  1300. fail:
  1301. goto out;
  1302. }
  1303. struct isofs_iget5_callback_data {
  1304. unsigned long block;
  1305. unsigned long offset;
  1306. };
  1307. static int isofs_iget5_test(struct inode *ino, void *data)
  1308. {
  1309. struct iso_inode_info *i = ISOFS_I(ino);
  1310. struct isofs_iget5_callback_data *d =
  1311. (struct isofs_iget5_callback_data*)data;
  1312. return (i->i_iget5_block == d->block)
  1313. && (i->i_iget5_offset == d->offset);
  1314. }
  1315. static int isofs_iget5_set(struct inode *ino, void *data)
  1316. {
  1317. struct iso_inode_info *i = ISOFS_I(ino);
  1318. struct isofs_iget5_callback_data *d =
  1319. (struct isofs_iget5_callback_data*)data;
  1320. i->i_iget5_block = d->block;
  1321. i->i_iget5_offset = d->offset;
  1322. return 0;
  1323. }
  1324. /* Store, in the inode's containing structure, the block and block
  1325. * offset that point to the underlying meta-data for the inode. The
  1326. * code below is otherwise similar to the iget() code in
  1327. * include/linux/fs.h */
  1328. struct inode *__isofs_iget(struct super_block *sb,
  1329. unsigned long block,
  1330. unsigned long offset,
  1331. int relocated)
  1332. {
  1333. unsigned long hashval;
  1334. struct inode *inode;
  1335. struct isofs_iget5_callback_data data;
  1336. long ret;
  1337. if (offset >= 1ul << sb->s_blocksize_bits)
  1338. return ERR_PTR(-EINVAL);
  1339. data.block = block;
  1340. data.offset = offset;
  1341. hashval = (block << sb->s_blocksize_bits) | offset;
  1342. inode = iget5_locked(sb, hashval, &isofs_iget5_test,
  1343. &isofs_iget5_set, &data);
  1344. if (!inode)
  1345. return ERR_PTR(-ENOMEM);
  1346. if (inode_state_read_once(inode) & I_NEW) {
  1347. ret = isofs_read_inode(inode, relocated);
  1348. if (ret < 0) {
  1349. iget_failed(inode);
  1350. inode = ERR_PTR(ret);
  1351. } else {
  1352. unlock_new_inode(inode);
  1353. }
  1354. }
  1355. return inode;
  1356. }
  1357. static int isofs_get_tree(struct fs_context *fc)
  1358. {
  1359. return get_tree_bdev(fc, isofs_fill_super);
  1360. }
  1361. static void isofs_free_fc(struct fs_context *fc)
  1362. {
  1363. struct isofs_options *opt = fc->fs_private;
  1364. kfree(opt->iocharset);
  1365. kfree(opt);
  1366. }
  1367. static const struct fs_context_operations isofs_context_ops = {
  1368. .parse_param = isofs_parse_param,
  1369. .get_tree = isofs_get_tree,
  1370. .reconfigure = isofs_reconfigure,
  1371. .free = isofs_free_fc,
  1372. };
  1373. static int isofs_init_fs_context(struct fs_context *fc)
  1374. {
  1375. struct isofs_options *opt;
  1376. opt = kzalloc_obj(*opt);
  1377. if (!opt)
  1378. return -ENOMEM;
  1379. opt->map = 'n';
  1380. opt->rock = 1;
  1381. opt->joliet = 1;
  1382. opt->cruft = 0;
  1383. opt->hide = 0;
  1384. opt->showassoc = 0;
  1385. opt->check = 'u'; /* unset */
  1386. opt->nocompress = 0;
  1387. opt->blocksize = 1024;
  1388. opt->fmode = opt->dmode = ISOFS_INVALID_MODE;
  1389. opt->uid_set = 0;
  1390. opt->gid_set = 0;
  1391. opt->gid = GLOBAL_ROOT_GID;
  1392. opt->uid = GLOBAL_ROOT_UID;
  1393. opt->iocharset = NULL;
  1394. opt->overriderockperm = 0;
  1395. opt->session = -1;
  1396. opt->sbsector = -1;
  1397. fc->fs_private = opt;
  1398. fc->ops = &isofs_context_ops;
  1399. return 0;
  1400. }
  1401. static struct file_system_type iso9660_fs_type = {
  1402. .owner = THIS_MODULE,
  1403. .name = "iso9660",
  1404. .kill_sb = kill_block_super,
  1405. .fs_flags = FS_REQUIRES_DEV,
  1406. .init_fs_context = isofs_init_fs_context,
  1407. .parameters = isofs_param_spec,
  1408. };
  1409. MODULE_ALIAS_FS("iso9660");
  1410. MODULE_ALIAS("iso9660");
  1411. static int __init init_iso9660_fs(void)
  1412. {
  1413. int err = init_inodecache();
  1414. if (err)
  1415. goto out;
  1416. #ifdef CONFIG_ZISOFS
  1417. err = zisofs_init();
  1418. if (err)
  1419. goto out1;
  1420. #endif
  1421. err = register_filesystem(&iso9660_fs_type);
  1422. if (err)
  1423. goto out2;
  1424. return 0;
  1425. out2:
  1426. #ifdef CONFIG_ZISOFS
  1427. zisofs_cleanup();
  1428. out1:
  1429. #endif
  1430. destroy_inodecache();
  1431. out:
  1432. return err;
  1433. }
  1434. static void __exit exit_iso9660_fs(void)
  1435. {
  1436. unregister_filesystem(&iso9660_fs_type);
  1437. #ifdef CONFIG_ZISOFS
  1438. zisofs_cleanup();
  1439. #endif
  1440. destroy_inodecache();
  1441. }
  1442. module_init(init_iso9660_fs)
  1443. module_exit(exit_iso9660_fs)
  1444. MODULE_DESCRIPTION("ISO 9660 CDROM file system support");
  1445. MODULE_LICENSE("GPL");