inode.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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(sizeof(*sbi), GFP_KERNEL);
  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. sbi->s_high_sierra = 0; /* default is iso9660 */
  546. sbi->s_session = opt->session;
  547. sbi->s_sbsector = opt->sbsector;
  548. vol_desc_start = (opt->sbsector != -1) ?
  549. opt->sbsector : isofs_get_last_session(s, opt->session);
  550. for (iso_blknum = vol_desc_start+16;
  551. iso_blknum < vol_desc_start+100; iso_blknum++) {
  552. struct hs_volume_descriptor *hdp;
  553. struct iso_volume_descriptor *vdp;
  554. block = iso_blknum << (ISOFS_BLOCK_BITS - s->s_blocksize_bits);
  555. if (!(bh = sb_bread(s, block)))
  556. goto out_no_read;
  557. vdp = (struct iso_volume_descriptor *)bh->b_data;
  558. hdp = (struct hs_volume_descriptor *)bh->b_data;
  559. /*
  560. * Due to the overlapping physical location of the descriptors,
  561. * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
  562. * proper identification in this case, we first check for ISO.
  563. */
  564. if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
  565. if (isonum_711(vdp->type) == ISO_VD_END)
  566. break;
  567. if (isonum_711(vdp->type) == ISO_VD_PRIMARY) {
  568. if (!pri) {
  569. pri = (struct iso_primary_descriptor *)vdp;
  570. /* Save the buffer in case we need it ... */
  571. pri_bh = bh;
  572. bh = NULL;
  573. }
  574. }
  575. #ifdef CONFIG_JOLIET
  576. else if (isonum_711(vdp->type) == ISO_VD_SUPPLEMENTARY) {
  577. sec = (struct iso_supplementary_descriptor *)vdp;
  578. if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
  579. if (opt->joliet) {
  580. if (sec->escape[2] == 0x40)
  581. joliet_level = 1;
  582. else if (sec->escape[2] == 0x43)
  583. joliet_level = 2;
  584. else if (sec->escape[2] == 0x45)
  585. joliet_level = 3;
  586. printk(KERN_DEBUG "ISO 9660 Extensions: "
  587. "Microsoft Joliet Level %d\n",
  588. joliet_level);
  589. }
  590. goto root_found;
  591. } else {
  592. /* Unknown supplementary volume descriptor */
  593. sec = NULL;
  594. }
  595. }
  596. #endif
  597. } else {
  598. if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
  599. if (isonum_711(hdp->type) != ISO_VD_PRIMARY)
  600. goto out_freebh;
  601. sbi->s_high_sierra = 1;
  602. opt->rock = 0;
  603. h_pri = (struct hs_primary_descriptor *)vdp;
  604. goto root_found;
  605. }
  606. }
  607. /* Just skip any volume descriptors we don't recognize */
  608. brelse(bh);
  609. bh = NULL;
  610. }
  611. /*
  612. * If we fall through, either no volume descriptor was found,
  613. * or else we passed a primary descriptor looking for others.
  614. */
  615. if (!pri)
  616. goto out_unknown_format;
  617. brelse(bh);
  618. bh = pri_bh;
  619. pri_bh = NULL;
  620. root_found:
  621. /* We don't support read-write mounts */
  622. if (!sb_rdonly(s)) {
  623. error = -EACCES;
  624. goto out_freebh;
  625. }
  626. if (joliet_level && (!pri || !opt->rock)) {
  627. /* This is the case of Joliet with the norock mount flag.
  628. * A disc with both Joliet and Rock Ridge is handled later
  629. */
  630. pri = (struct iso_primary_descriptor *) sec;
  631. }
  632. if(sbi->s_high_sierra){
  633. rootp = (struct iso_directory_record *) h_pri->root_directory_record;
  634. sbi->s_nzones = isonum_733(h_pri->volume_space_size);
  635. sbi->s_log_zone_size = isonum_723(h_pri->logical_block_size);
  636. sbi->s_max_size = isonum_733(h_pri->volume_space_size);
  637. } else {
  638. if (!pri)
  639. goto out_freebh;
  640. rootp = (struct iso_directory_record *) pri->root_directory_record;
  641. sbi->s_nzones = isonum_733(pri->volume_space_size);
  642. sbi->s_log_zone_size = isonum_723(pri->logical_block_size);
  643. sbi->s_max_size = isonum_733(pri->volume_space_size);
  644. }
  645. sbi->s_ninodes = 0; /* No way to figure this out easily */
  646. orig_zonesize = sbi->s_log_zone_size;
  647. /*
  648. * If the zone size is smaller than the hardware sector size,
  649. * this is a fatal error. This would occur if the disc drive
  650. * had sectors that were 2048 bytes, but the filesystem had
  651. * blocks that were 512 bytes (which should only very rarely
  652. * happen.)
  653. */
  654. if (orig_zonesize < opt->blocksize)
  655. goto out_bad_size;
  656. /* RDE: convert log zone size to bit shift */
  657. switch (sbi->s_log_zone_size) {
  658. case 512: sbi->s_log_zone_size = 9; break;
  659. case 1024: sbi->s_log_zone_size = 10; break;
  660. case 2048: sbi->s_log_zone_size = 11; break;
  661. default:
  662. goto out_bad_zone_size;
  663. }
  664. s->s_magic = ISOFS_SUPER_MAGIC;
  665. /*
  666. * With multi-extent files, file size is only limited by the maximum
  667. * size of a file system, which is 8 TB.
  668. */
  669. s->s_maxbytes = 0x80000000000LL;
  670. /* ECMA-119 timestamp from 1900/1/1 with tz offset */
  671. s->s_time_min = mktime64(1900, 1, 1, 0, 0, 0) - MAX_TZ_OFFSET;
  672. s->s_time_max = mktime64(U8_MAX+1900, 12, 31, 23, 59, 59) + MAX_TZ_OFFSET;
  673. /* Set this for reference. Its not currently used except on write
  674. which we don't have .. */
  675. first_data_zone = isonum_733(rootp->extent) +
  676. isonum_711(rootp->ext_attr_length);
  677. sbi->s_firstdatazone = first_data_zone;
  678. #ifndef BEQUIET
  679. printk(KERN_DEBUG "ISOFS: Max size:%ld Log zone size:%ld\n",
  680. sbi->s_max_size, 1UL << sbi->s_log_zone_size);
  681. printk(KERN_DEBUG "ISOFS: First datazone:%ld\n", sbi->s_firstdatazone);
  682. if(sbi->s_high_sierra)
  683. printk(KERN_DEBUG "ISOFS: Disc in High Sierra format.\n");
  684. #endif
  685. /*
  686. * If the Joliet level is set, we _may_ decide to use the
  687. * secondary descriptor, but can't be sure until after we
  688. * read the root inode. But before reading the root inode
  689. * we may need to change the device blocksize, and would
  690. * rather release the old buffer first. So, we cache the
  691. * first_data_zone value from the secondary descriptor.
  692. */
  693. if (joliet_level) {
  694. pri = (struct iso_primary_descriptor *) sec;
  695. rootp = (struct iso_directory_record *)
  696. pri->root_directory_record;
  697. first_data_zone = isonum_733(rootp->extent) +
  698. isonum_711(rootp->ext_attr_length);
  699. }
  700. /*
  701. * We're all done using the volume descriptor, and may need
  702. * to change the device blocksize, so release the buffer now.
  703. */
  704. brelse(pri_bh);
  705. brelse(bh);
  706. /*
  707. * Force the blocksize to 512 for 512 byte sectors. The file
  708. * read primitives really get it wrong in a bad way if we don't
  709. * do this.
  710. *
  711. * Note - we should never be setting the blocksize to something
  712. * less than the hardware sector size for the device. If we
  713. * do, we would end up having to read larger buffers and split
  714. * out portions to satisfy requests.
  715. *
  716. * Note2- the idea here is that we want to deal with the optimal
  717. * zonesize in the filesystem. If we have it set to something less,
  718. * then we have horrible problems with trying to piece together
  719. * bits of adjacent blocks in order to properly read directory
  720. * entries. By forcing the blocksize in this way, we ensure
  721. * that we will never be required to do this.
  722. */
  723. sb_set_blocksize(s, orig_zonesize);
  724. sbi->s_nls_iocharset = NULL;
  725. #ifdef CONFIG_JOLIET
  726. if (joliet_level) {
  727. char *p = opt->iocharset ? opt->iocharset : CONFIG_NLS_DEFAULT;
  728. if (strcmp(p, "utf8") != 0) {
  729. sbi->s_nls_iocharset = opt->iocharset ?
  730. load_nls(opt->iocharset) : load_nls_default();
  731. if (!sbi->s_nls_iocharset)
  732. goto out_freesbi;
  733. }
  734. }
  735. #endif
  736. s->s_op = &isofs_sops;
  737. s->s_export_op = &isofs_export_ops;
  738. sbi->s_mapping = opt->map;
  739. sbi->s_rock = (opt->rock ? 2 : 0);
  740. sbi->s_rock_offset = -1; /* initial offset, will guess until SP is found*/
  741. sbi->s_cruft = opt->cruft;
  742. sbi->s_hide = opt->hide;
  743. sbi->s_showassoc = opt->showassoc;
  744. sbi->s_uid = opt->uid;
  745. sbi->s_gid = opt->gid;
  746. sbi->s_uid_set = opt->uid_set;
  747. sbi->s_gid_set = opt->gid_set;
  748. sbi->s_nocompress = opt->nocompress;
  749. sbi->s_overriderockperm = opt->overriderockperm;
  750. /*
  751. * It would be incredibly stupid to allow people to mark every file
  752. * on the disk as suid, so we merely allow them to set the default
  753. * permissions.
  754. */
  755. if (opt->fmode != ISOFS_INVALID_MODE)
  756. sbi->s_fmode = opt->fmode & 0777;
  757. else
  758. sbi->s_fmode = ISOFS_INVALID_MODE;
  759. if (opt->dmode != ISOFS_INVALID_MODE)
  760. sbi->s_dmode = opt->dmode & 0777;
  761. else
  762. sbi->s_dmode = ISOFS_INVALID_MODE;
  763. /*
  764. * Read the root inode, which _may_ result in changing
  765. * the s_rock flag. Once we have the final s_rock value,
  766. * we then decide whether to use the Joliet descriptor.
  767. */
  768. inode = isofs_iget(s, sbi->s_firstdatazone, 0);
  769. /*
  770. * Fix for broken CDs with a corrupt root inode but a correct Joliet
  771. * root directory.
  772. */
  773. if (IS_ERR(inode)) {
  774. if (joliet_level && sbi->s_firstdatazone != first_data_zone) {
  775. printk(KERN_NOTICE
  776. "ISOFS: root inode is unusable. "
  777. "Disabling Rock Ridge and switching to Joliet.");
  778. sbi->s_rock = 0;
  779. inode = NULL;
  780. } else {
  781. goto out_no_root;
  782. }
  783. }
  784. /*
  785. * Fix for broken CDs with Rock Ridge and empty ISO root directory but
  786. * correct Joliet root directory.
  787. */
  788. if (sbi->s_rock == 1 && joliet_level &&
  789. rootdir_empty(s, sbi->s_firstdatazone)) {
  790. printk(KERN_NOTICE
  791. "ISOFS: primary root directory is empty. "
  792. "Disabling Rock Ridge and switching to Joliet.");
  793. sbi->s_rock = 0;
  794. }
  795. /*
  796. * If this disk has both Rock Ridge and Joliet on it, then we
  797. * want to use Rock Ridge by default. This can be overridden
  798. * by using the norock mount option. There is still one other
  799. * possibility that is not taken into account: a Rock Ridge
  800. * CD with Unicode names. Until someone sees such a beast, it
  801. * will not be supported.
  802. */
  803. if (sbi->s_rock == 1) {
  804. joliet_level = 0;
  805. } else if (joliet_level) {
  806. sbi->s_rock = 0;
  807. if (sbi->s_firstdatazone != first_data_zone) {
  808. sbi->s_firstdatazone = first_data_zone;
  809. printk(KERN_DEBUG
  810. "ISOFS: changing to secondary root\n");
  811. iput(inode);
  812. inode = isofs_iget(s, sbi->s_firstdatazone, 0);
  813. if (IS_ERR(inode))
  814. goto out_no_root;
  815. }
  816. }
  817. if (opt->check == 'u') {
  818. /* Only Joliet is case insensitive by default */
  819. if (joliet_level)
  820. opt->check = 'r';
  821. else
  822. opt->check = 's';
  823. }
  824. sbi->s_joliet_level = joliet_level;
  825. /* Make sure the root inode is a directory */
  826. if (!S_ISDIR(inode->i_mode)) {
  827. printk(KERN_WARNING
  828. "isofs_fill_super: root inode is not a directory. "
  829. "Corrupted media?\n");
  830. goto out_iput;
  831. }
  832. table = 0;
  833. if (joliet_level)
  834. table += 2;
  835. if (opt->check == 'r')
  836. table++;
  837. sbi->s_check = opt->check;
  838. if (table)
  839. s->s_d_op = &isofs_dentry_ops[table - 1];
  840. /* get the root dentry */
  841. s->s_root = d_make_root(inode);
  842. if (!(s->s_root)) {
  843. error = -ENOMEM;
  844. goto out_no_inode;
  845. }
  846. return 0;
  847. /*
  848. * Display error messages and free resources.
  849. */
  850. out_iput:
  851. iput(inode);
  852. goto out_no_inode;
  853. out_no_root:
  854. error = PTR_ERR(inode);
  855. if (error != -ENOMEM)
  856. printk(KERN_WARNING "%s: get root inode failed\n", __func__);
  857. out_no_inode:
  858. #ifdef CONFIG_JOLIET
  859. unload_nls(sbi->s_nls_iocharset);
  860. #endif
  861. goto out_freesbi;
  862. out_no_read:
  863. printk(KERN_WARNING "%s: bread failed, dev=%s, iso_blknum=%d, block=%d\n",
  864. __func__, s->s_id, iso_blknum, block);
  865. goto out_freebh;
  866. out_bad_zone_size:
  867. printk(KERN_WARNING "ISOFS: Bad logical zone size %ld\n",
  868. sbi->s_log_zone_size);
  869. goto out_freebh;
  870. out_bad_size:
  871. printk(KERN_WARNING "ISOFS: Logical zone size(%d) < hardware blocksize(%u)\n",
  872. orig_zonesize, opt->blocksize);
  873. goto out_freebh;
  874. out_unknown_format:
  875. if (!silent)
  876. printk(KERN_WARNING "ISOFS: Unable to identify CD-ROM format.\n");
  877. out_freebh:
  878. brelse(bh);
  879. brelse(pri_bh);
  880. out_freesbi:
  881. kfree(sbi);
  882. s->s_fs_info = NULL;
  883. return error;
  884. }
  885. static int isofs_statfs (struct dentry *dentry, struct kstatfs *buf)
  886. {
  887. struct super_block *sb = dentry->d_sb;
  888. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  889. buf->f_type = ISOFS_SUPER_MAGIC;
  890. buf->f_bsize = sb->s_blocksize;
  891. buf->f_blocks = (ISOFS_SB(sb)->s_nzones
  892. << (ISOFS_SB(sb)->s_log_zone_size - sb->s_blocksize_bits));
  893. buf->f_bfree = 0;
  894. buf->f_bavail = 0;
  895. buf->f_files = ISOFS_SB(sb)->s_ninodes;
  896. buf->f_ffree = 0;
  897. buf->f_fsid = u64_to_fsid(id);
  898. buf->f_namelen = NAME_MAX;
  899. return 0;
  900. }
  901. /*
  902. * Get a set of blocks; filling in buffer_heads if already allocated
  903. * or getblk() if they are not. Returns the number of blocks inserted
  904. * (-ve == error.)
  905. */
  906. int isofs_get_blocks(struct inode *inode, sector_t iblock,
  907. struct buffer_head **bh, unsigned long nblocks)
  908. {
  909. unsigned long b_off = iblock;
  910. unsigned offset, sect_size;
  911. unsigned int firstext;
  912. unsigned long nextblk, nextoff;
  913. int section, rv, error;
  914. struct iso_inode_info *ei = ISOFS_I(inode);
  915. error = -EIO;
  916. rv = 0;
  917. if (iblock != b_off) {
  918. printk(KERN_DEBUG "%s: block number too large\n", __func__);
  919. goto abort;
  920. }
  921. offset = 0;
  922. firstext = ei->i_first_extent;
  923. sect_size = ei->i_section_size >> ISOFS_BUFFER_BITS(inode);
  924. nextblk = ei->i_next_section_block;
  925. nextoff = ei->i_next_section_offset;
  926. section = 0;
  927. while (nblocks) {
  928. /* If we are *way* beyond the end of the file, print a message.
  929. * Access beyond the end of the file up to the next page boundary
  930. * is normal, however because of the way the page cache works.
  931. * In this case, we just return 0 so that we can properly fill
  932. * the page with useless information without generating any
  933. * I/O errors.
  934. */
  935. if (b_off > ((inode->i_size + PAGE_SIZE - 1) >> ISOFS_BUFFER_BITS(inode))) {
  936. printk(KERN_DEBUG "%s: block >= EOF (%lu, %llu)\n",
  937. __func__, b_off,
  938. (unsigned long long)inode->i_size);
  939. goto abort;
  940. }
  941. /* On the last section, nextblk == 0, section size is likely to
  942. * exceed sect_size by a partial block, and access beyond the
  943. * end of the file will reach beyond the section size, too.
  944. */
  945. while (nextblk && (b_off >= (offset + sect_size))) {
  946. struct inode *ninode;
  947. offset += sect_size;
  948. ninode = isofs_iget(inode->i_sb, nextblk, nextoff);
  949. if (IS_ERR(ninode)) {
  950. error = PTR_ERR(ninode);
  951. goto abort;
  952. }
  953. firstext = ISOFS_I(ninode)->i_first_extent;
  954. sect_size = ISOFS_I(ninode)->i_section_size >> ISOFS_BUFFER_BITS(ninode);
  955. nextblk = ISOFS_I(ninode)->i_next_section_block;
  956. nextoff = ISOFS_I(ninode)->i_next_section_offset;
  957. iput(ninode);
  958. if (++section > 100) {
  959. printk(KERN_DEBUG "%s: More than 100 file sections ?!?"
  960. " aborting...\n", __func__);
  961. printk(KERN_DEBUG "%s: block=%lu firstext=%u sect_size=%u "
  962. "nextblk=%lu nextoff=%lu\n", __func__,
  963. b_off, firstext, (unsigned) sect_size,
  964. nextblk, nextoff);
  965. goto abort;
  966. }
  967. }
  968. if (*bh) {
  969. map_bh(*bh, inode->i_sb, firstext + b_off - offset);
  970. } else {
  971. *bh = sb_getblk(inode->i_sb, firstext+b_off-offset);
  972. if (!*bh)
  973. goto abort;
  974. }
  975. bh++; /* Next buffer head */
  976. b_off++; /* Next buffer offset */
  977. nblocks--;
  978. rv++;
  979. }
  980. error = 0;
  981. abort:
  982. return rv != 0 ? rv : error;
  983. }
  984. /*
  985. * Used by the standard interfaces.
  986. */
  987. static int isofs_get_block(struct inode *inode, sector_t iblock,
  988. struct buffer_head *bh_result, int create)
  989. {
  990. int ret;
  991. if (create) {
  992. printk(KERN_DEBUG "%s: Kernel tries to allocate a block\n", __func__);
  993. return -EROFS;
  994. }
  995. ret = isofs_get_blocks(inode, iblock, &bh_result, 1);
  996. return ret < 0 ? ret : 0;
  997. }
  998. static int isofs_bmap(struct inode *inode, sector_t block)
  999. {
  1000. struct buffer_head dummy;
  1001. int error;
  1002. dummy.b_state = 0;
  1003. dummy.b_blocknr = -1000;
  1004. error = isofs_get_block(inode, block, &dummy, 0);
  1005. if (!error)
  1006. return dummy.b_blocknr;
  1007. return 0;
  1008. }
  1009. struct buffer_head *isofs_bread(struct inode *inode, sector_t block)
  1010. {
  1011. sector_t blknr = isofs_bmap(inode, block);
  1012. if (!blknr)
  1013. return NULL;
  1014. return sb_bread(inode->i_sb, blknr);
  1015. }
  1016. static int isofs_read_folio(struct file *file, struct folio *folio)
  1017. {
  1018. return mpage_read_folio(folio, isofs_get_block);
  1019. }
  1020. static void isofs_readahead(struct readahead_control *rac)
  1021. {
  1022. mpage_readahead(rac, isofs_get_block);
  1023. }
  1024. static sector_t _isofs_bmap(struct address_space *mapping, sector_t block)
  1025. {
  1026. return generic_block_bmap(mapping,block,isofs_get_block);
  1027. }
  1028. static const struct address_space_operations isofs_aops = {
  1029. .read_folio = isofs_read_folio,
  1030. .readahead = isofs_readahead,
  1031. .bmap = _isofs_bmap
  1032. };
  1033. static int isofs_read_level3_size(struct inode *inode)
  1034. {
  1035. unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  1036. int high_sierra = ISOFS_SB(inode->i_sb)->s_high_sierra;
  1037. struct buffer_head *bh = NULL;
  1038. unsigned long block, offset, block_saved, offset_saved;
  1039. int i = 0;
  1040. int more_entries = 0;
  1041. struct iso_directory_record *tmpde = NULL;
  1042. struct iso_inode_info *ei = ISOFS_I(inode);
  1043. inode->i_size = 0;
  1044. /* The first 16 blocks are reserved as the System Area. Thus,
  1045. * no inodes can appear in block 0. We use this to flag that
  1046. * this is the last section. */
  1047. ei->i_next_section_block = 0;
  1048. ei->i_next_section_offset = 0;
  1049. block = ei->i_iget5_block;
  1050. offset = ei->i_iget5_offset;
  1051. do {
  1052. struct iso_directory_record *de;
  1053. unsigned int de_len;
  1054. if (!bh) {
  1055. bh = sb_bread(inode->i_sb, block);
  1056. if (!bh)
  1057. goto out_noread;
  1058. }
  1059. de = (struct iso_directory_record *) (bh->b_data + offset);
  1060. de_len = *(unsigned char *) de;
  1061. if (de_len == 0) {
  1062. brelse(bh);
  1063. bh = NULL;
  1064. ++block;
  1065. offset = 0;
  1066. continue;
  1067. }
  1068. block_saved = block;
  1069. offset_saved = offset;
  1070. offset += de_len;
  1071. /* Make sure we have a full directory entry */
  1072. if (offset >= bufsize) {
  1073. int slop = bufsize - offset + de_len;
  1074. if (!tmpde) {
  1075. tmpde = kmalloc(256, GFP_KERNEL);
  1076. if (!tmpde)
  1077. goto out_nomem;
  1078. }
  1079. memcpy(tmpde, de, slop);
  1080. offset &= bufsize - 1;
  1081. block++;
  1082. brelse(bh);
  1083. bh = NULL;
  1084. if (offset) {
  1085. bh = sb_bread(inode->i_sb, block);
  1086. if (!bh)
  1087. goto out_noread;
  1088. memcpy((void *)tmpde+slop, bh->b_data, offset);
  1089. }
  1090. de = tmpde;
  1091. }
  1092. inode->i_size += isonum_733(de->size);
  1093. if (i == 1) {
  1094. ei->i_next_section_block = block_saved;
  1095. ei->i_next_section_offset = offset_saved;
  1096. }
  1097. more_entries = de->flags[-high_sierra] & 0x80;
  1098. i++;
  1099. if (i > 100)
  1100. goto out_toomany;
  1101. } while (more_entries);
  1102. out:
  1103. kfree(tmpde);
  1104. brelse(bh);
  1105. return 0;
  1106. out_nomem:
  1107. brelse(bh);
  1108. return -ENOMEM;
  1109. out_noread:
  1110. printk(KERN_INFO "ISOFS: unable to read i-node block %lu\n", block);
  1111. kfree(tmpde);
  1112. return -EIO;
  1113. out_toomany:
  1114. printk(KERN_INFO "%s: More than 100 file sections ?!?, aborting...\n"
  1115. "isofs_read_level3_size: inode=%lu\n",
  1116. __func__, inode->i_ino);
  1117. goto out;
  1118. }
  1119. static int isofs_read_inode(struct inode *inode, int relocated)
  1120. {
  1121. struct super_block *sb = inode->i_sb;
  1122. struct isofs_sb_info *sbi = ISOFS_SB(sb);
  1123. unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
  1124. unsigned long block;
  1125. int high_sierra = sbi->s_high_sierra;
  1126. struct buffer_head *bh;
  1127. struct iso_directory_record *de;
  1128. struct iso_directory_record *tmpde = NULL;
  1129. unsigned int de_len;
  1130. unsigned long offset;
  1131. struct iso_inode_info *ei = ISOFS_I(inode);
  1132. int ret = -EIO;
  1133. struct timespec64 ts;
  1134. block = ei->i_iget5_block;
  1135. bh = sb_bread(inode->i_sb, block);
  1136. if (!bh)
  1137. goto out_badread;
  1138. offset = ei->i_iget5_offset;
  1139. de = (struct iso_directory_record *) (bh->b_data + offset);
  1140. de_len = *(unsigned char *) de;
  1141. if (de_len < sizeof(struct iso_directory_record))
  1142. goto fail;
  1143. if (offset + de_len > bufsize) {
  1144. int frag1 = bufsize - offset;
  1145. tmpde = kmalloc(de_len, GFP_KERNEL);
  1146. if (!tmpde) {
  1147. ret = -ENOMEM;
  1148. goto fail;
  1149. }
  1150. memcpy(tmpde, bh->b_data + offset, frag1);
  1151. brelse(bh);
  1152. bh = sb_bread(inode->i_sb, ++block);
  1153. if (!bh)
  1154. goto out_badread;
  1155. memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
  1156. de = tmpde;
  1157. }
  1158. inode->i_ino = isofs_get_ino(ei->i_iget5_block,
  1159. ei->i_iget5_offset,
  1160. ISOFS_BUFFER_BITS(inode));
  1161. /* Assume it is a normal-format file unless told otherwise */
  1162. ei->i_file_format = isofs_file_normal;
  1163. if (de->flags[-high_sierra] & 2) {
  1164. if (sbi->s_dmode != ISOFS_INVALID_MODE)
  1165. inode->i_mode = S_IFDIR | sbi->s_dmode;
  1166. else
  1167. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  1168. set_nlink(inode, 1); /*
  1169. * Set to 1. We know there are 2, but
  1170. * the find utility tries to optimize
  1171. * if it is 2, and it screws up. It is
  1172. * easier to give 1 which tells find to
  1173. * do it the hard way.
  1174. */
  1175. } else {
  1176. if (sbi->s_fmode != ISOFS_INVALID_MODE) {
  1177. inode->i_mode = S_IFREG | sbi->s_fmode;
  1178. } else {
  1179. /*
  1180. * Set default permissions: r-x for all. The disc
  1181. * could be shared with DOS machines so virtually
  1182. * anything could be a valid executable.
  1183. */
  1184. inode->i_mode = S_IFREG | S_IRUGO | S_IXUGO;
  1185. }
  1186. set_nlink(inode, 1);
  1187. }
  1188. inode->i_uid = sbi->s_uid;
  1189. inode->i_gid = sbi->s_gid;
  1190. inode->i_blocks = 0;
  1191. ei->i_format_parm[0] = 0;
  1192. ei->i_format_parm[1] = 0;
  1193. ei->i_format_parm[2] = 0;
  1194. ei->i_section_size = isonum_733(de->size);
  1195. if (de->flags[-high_sierra] & 0x80) {
  1196. ret = isofs_read_level3_size(inode);
  1197. if (ret < 0)
  1198. goto fail;
  1199. ret = -EIO;
  1200. } else {
  1201. ei->i_next_section_block = 0;
  1202. ei->i_next_section_offset = 0;
  1203. inode->i_size = isonum_733(de->size);
  1204. }
  1205. /*
  1206. * Some dipshit decided to store some other bit of information
  1207. * in the high byte of the file length. Truncate size in case
  1208. * this CDROM was mounted with the cruft option.
  1209. */
  1210. if (sbi->s_cruft)
  1211. inode->i_size &= 0x00ffffff;
  1212. if (de->interleave[0]) {
  1213. printk(KERN_DEBUG "ISOFS: Interleaved files not (yet) supported.\n");
  1214. inode->i_size = 0;
  1215. }
  1216. /* I have no idea what file_unit_size is used for, so
  1217. we will flag it for now */
  1218. if (de->file_unit_size[0] != 0) {
  1219. printk(KERN_DEBUG "ISOFS: File unit size != 0 for ISO file (%ld).\n",
  1220. inode->i_ino);
  1221. }
  1222. /* I have no idea what other flag bits are used for, so
  1223. we will flag it for now */
  1224. #ifdef DEBUG
  1225. if((de->flags[-high_sierra] & ~2)!= 0){
  1226. printk(KERN_DEBUG "ISOFS: Unusual flag settings for ISO file "
  1227. "(%ld %x).\n",
  1228. inode->i_ino, de->flags[-high_sierra]);
  1229. }
  1230. #endif
  1231. ts = iso_date(de->date, high_sierra ? ISO_DATE_HIGH_SIERRA : 0);
  1232. inode_set_ctime_to_ts(inode, ts);
  1233. inode_set_atime_to_ts(inode, ts);
  1234. inode_set_mtime_to_ts(inode, ts);
  1235. ei->i_first_extent = (isonum_733(de->extent) +
  1236. isonum_711(de->ext_attr_length));
  1237. /* Set the number of blocks for stat() - should be done before RR */
  1238. inode->i_blocks = (inode->i_size + 511) >> 9;
  1239. /*
  1240. * Now test for possible Rock Ridge extensions which will override
  1241. * some of these numbers in the inode structure.
  1242. */
  1243. if (!high_sierra) {
  1244. parse_rock_ridge_inode(de, inode, relocated);
  1245. /* if we want uid/gid set, override the rock ridge setting */
  1246. if (sbi->s_uid_set)
  1247. inode->i_uid = sbi->s_uid;
  1248. if (sbi->s_gid_set)
  1249. inode->i_gid = sbi->s_gid;
  1250. }
  1251. /* Now set final access rights if overriding rock ridge setting */
  1252. if (S_ISDIR(inode->i_mode) && sbi->s_overriderockperm &&
  1253. sbi->s_dmode != ISOFS_INVALID_MODE)
  1254. inode->i_mode = S_IFDIR | sbi->s_dmode;
  1255. if (S_ISREG(inode->i_mode) && sbi->s_overriderockperm &&
  1256. sbi->s_fmode != ISOFS_INVALID_MODE)
  1257. inode->i_mode = S_IFREG | sbi->s_fmode;
  1258. /* Install the inode operations vector */
  1259. if (S_ISREG(inode->i_mode)) {
  1260. inode->i_fop = &generic_ro_fops;
  1261. switch (ei->i_file_format) {
  1262. #ifdef CONFIG_ZISOFS
  1263. case isofs_file_compressed:
  1264. inode->i_data.a_ops = &zisofs_aops;
  1265. break;
  1266. #endif
  1267. default:
  1268. inode->i_data.a_ops = &isofs_aops;
  1269. break;
  1270. }
  1271. } else if (S_ISDIR(inode->i_mode)) {
  1272. inode->i_op = &isofs_dir_inode_operations;
  1273. inode->i_fop = &isofs_dir_operations;
  1274. } else if (S_ISLNK(inode->i_mode)) {
  1275. inode->i_op = &page_symlink_inode_operations;
  1276. inode_nohighmem(inode);
  1277. inode->i_data.a_ops = &isofs_symlink_aops;
  1278. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  1279. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1280. /* XXX - parse_rock_ridge_inode() had already set i_rdev. */
  1281. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  1282. } else {
  1283. printk(KERN_DEBUG "ISOFS: Invalid file type 0%04o for inode %lu.\n",
  1284. inode->i_mode, inode->i_ino);
  1285. ret = -EIO;
  1286. goto fail;
  1287. }
  1288. ret = 0;
  1289. out:
  1290. kfree(tmpde);
  1291. brelse(bh);
  1292. return ret;
  1293. out_badread:
  1294. printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
  1295. fail:
  1296. goto out;
  1297. }
  1298. struct isofs_iget5_callback_data {
  1299. unsigned long block;
  1300. unsigned long offset;
  1301. };
  1302. static int isofs_iget5_test(struct inode *ino, void *data)
  1303. {
  1304. struct iso_inode_info *i = ISOFS_I(ino);
  1305. struct isofs_iget5_callback_data *d =
  1306. (struct isofs_iget5_callback_data*)data;
  1307. return (i->i_iget5_block == d->block)
  1308. && (i->i_iget5_offset == d->offset);
  1309. }
  1310. static int isofs_iget5_set(struct inode *ino, void *data)
  1311. {
  1312. struct iso_inode_info *i = ISOFS_I(ino);
  1313. struct isofs_iget5_callback_data *d =
  1314. (struct isofs_iget5_callback_data*)data;
  1315. i->i_iget5_block = d->block;
  1316. i->i_iget5_offset = d->offset;
  1317. return 0;
  1318. }
  1319. /* Store, in the inode's containing structure, the block and block
  1320. * offset that point to the underlying meta-data for the inode. The
  1321. * code below is otherwise similar to the iget() code in
  1322. * include/linux/fs.h */
  1323. struct inode *__isofs_iget(struct super_block *sb,
  1324. unsigned long block,
  1325. unsigned long offset,
  1326. int relocated)
  1327. {
  1328. unsigned long hashval;
  1329. struct inode *inode;
  1330. struct isofs_iget5_callback_data data;
  1331. long ret;
  1332. if (offset >= 1ul << sb->s_blocksize_bits)
  1333. return ERR_PTR(-EINVAL);
  1334. data.block = block;
  1335. data.offset = offset;
  1336. hashval = (block << sb->s_blocksize_bits) | offset;
  1337. inode = iget5_locked(sb, hashval, &isofs_iget5_test,
  1338. &isofs_iget5_set, &data);
  1339. if (!inode)
  1340. return ERR_PTR(-ENOMEM);
  1341. if (inode->i_state & I_NEW) {
  1342. ret = isofs_read_inode(inode, relocated);
  1343. if (ret < 0) {
  1344. iget_failed(inode);
  1345. inode = ERR_PTR(ret);
  1346. } else {
  1347. unlock_new_inode(inode);
  1348. }
  1349. }
  1350. return inode;
  1351. }
  1352. static int isofs_get_tree(struct fs_context *fc)
  1353. {
  1354. return get_tree_bdev(fc, isofs_fill_super);
  1355. }
  1356. static void isofs_free_fc(struct fs_context *fc)
  1357. {
  1358. struct isofs_options *opt = fc->fs_private;
  1359. kfree(opt->iocharset);
  1360. kfree(opt);
  1361. }
  1362. static const struct fs_context_operations isofs_context_ops = {
  1363. .parse_param = isofs_parse_param,
  1364. .get_tree = isofs_get_tree,
  1365. .reconfigure = isofs_reconfigure,
  1366. .free = isofs_free_fc,
  1367. };
  1368. static int isofs_init_fs_context(struct fs_context *fc)
  1369. {
  1370. struct isofs_options *opt;
  1371. opt = kzalloc(sizeof(*opt), GFP_KERNEL);
  1372. if (!opt)
  1373. return -ENOMEM;
  1374. opt->map = 'n';
  1375. opt->rock = 1;
  1376. opt->joliet = 1;
  1377. opt->cruft = 0;
  1378. opt->hide = 0;
  1379. opt->showassoc = 0;
  1380. opt->check = 'u'; /* unset */
  1381. opt->nocompress = 0;
  1382. opt->blocksize = 1024;
  1383. opt->fmode = opt->dmode = ISOFS_INVALID_MODE;
  1384. opt->uid_set = 0;
  1385. opt->gid_set = 0;
  1386. opt->gid = GLOBAL_ROOT_GID;
  1387. opt->uid = GLOBAL_ROOT_UID;
  1388. opt->iocharset = NULL;
  1389. opt->overriderockperm = 0;
  1390. opt->session = -1;
  1391. opt->sbsector = -1;
  1392. fc->fs_private = opt;
  1393. fc->ops = &isofs_context_ops;
  1394. return 0;
  1395. }
  1396. static struct file_system_type iso9660_fs_type = {
  1397. .owner = THIS_MODULE,
  1398. .name = "iso9660",
  1399. .kill_sb = kill_block_super,
  1400. .fs_flags = FS_REQUIRES_DEV,
  1401. .init_fs_context = isofs_init_fs_context,
  1402. .parameters = isofs_param_spec,
  1403. };
  1404. MODULE_ALIAS_FS("iso9660");
  1405. MODULE_ALIAS("iso9660");
  1406. static int __init init_iso9660_fs(void)
  1407. {
  1408. int err = init_inodecache();
  1409. if (err)
  1410. goto out;
  1411. #ifdef CONFIG_ZISOFS
  1412. err = zisofs_init();
  1413. if (err)
  1414. goto out1;
  1415. #endif
  1416. err = register_filesystem(&iso9660_fs_type);
  1417. if (err)
  1418. goto out2;
  1419. return 0;
  1420. out2:
  1421. #ifdef CONFIG_ZISOFS
  1422. zisofs_cleanup();
  1423. out1:
  1424. #endif
  1425. destroy_inodecache();
  1426. out:
  1427. return err;
  1428. }
  1429. static void __exit exit_iso9660_fs(void)
  1430. {
  1431. unregister_filesystem(&iso9660_fs_type);
  1432. #ifdef CONFIG_ZISOFS
  1433. zisofs_cleanup();
  1434. #endif
  1435. destroy_inodecache();
  1436. }
  1437. module_init(init_iso9660_fs)
  1438. module_exit(exit_iso9660_fs)
  1439. MODULE_DESCRIPTION("ISO 9660 CDROM file system support");
  1440. MODULE_LICENSE("GPL");