super.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  1. /*
  2. * super.c
  3. *
  4. * PURPOSE
  5. * Super block routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * DESCRIPTION
  8. * OSTA-UDF(tm) = Optical Storage Technology Association
  9. * Universal Disk Format.
  10. *
  11. * This code is based on version 2.00 of the UDF specification,
  12. * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
  13. * http://www.osta.org/
  14. * http://www.ecma.ch/
  15. * http://www.iso.org/
  16. *
  17. * COPYRIGHT
  18. * This file is distributed under the terms of the GNU General Public
  19. * License (GPL). Copies of the GPL can be obtained from:
  20. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  21. * Each contributing author retains all rights to their own work.
  22. *
  23. * (C) 1998 Dave Boynton
  24. * (C) 1998-2004 Ben Fennema
  25. * (C) 2000 Stelias Computing Inc
  26. *
  27. * HISTORY
  28. *
  29. * 09/24/98 dgb changed to allow compiling outside of kernel, and
  30. * added some debugging.
  31. * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
  32. * 10/16/98 attempting some multi-session support
  33. * 10/17/98 added freespace count for "df"
  34. * 11/11/98 gr added novrs option
  35. * 11/26/98 dgb added fileset,anchor mount options
  36. * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
  37. * vol descs. rewrote option handling based on isofs
  38. * 12/20/98 find the free space bitmap (if it exists)
  39. */
  40. #include "udfdecl.h"
  41. #include <linux/blkdev.h>
  42. #include <linux/slab.h>
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/parser.h>
  46. #include <linux/stat.h>
  47. #include <linux/cdrom.h>
  48. #include <linux/nls.h>
  49. #include <linux/buffer_head.h>
  50. #include <linux/vfs.h>
  51. #include <linux/vmalloc.h>
  52. #include <linux/errno.h>
  53. #include <linux/mount.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/bitmap.h>
  56. #include <linux/crc-itu-t.h>
  57. #include <asm/byteorder.h>
  58. #include "udf_sb.h"
  59. #include "udf_i.h"
  60. #include <linux/init.h>
  61. #include <asm/uaccess.h>
  62. #define VDS_POS_PRIMARY_VOL_DESC 0
  63. #define VDS_POS_UNALLOC_SPACE_DESC 1
  64. #define VDS_POS_LOGICAL_VOL_DESC 2
  65. #define VDS_POS_PARTITION_DESC 3
  66. #define VDS_POS_IMP_USE_VOL_DESC 4
  67. #define VDS_POS_VOL_DESC_PTR 5
  68. #define VDS_POS_TERMINATING_DESC 6
  69. #define VDS_POS_LENGTH 7
  70. #define UDF_DEFAULT_BLOCKSIZE 2048
  71. static char error_buf[1024];
  72. /* These are the "meat" - everything else is stuffing */
  73. static int udf_fill_super(struct super_block *, void *, int);
  74. static void udf_put_super(struct super_block *);
  75. static int udf_sync_fs(struct super_block *, int);
  76. static int udf_remount_fs(struct super_block *, int *, char *);
  77. static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
  78. static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
  79. struct kernel_lb_addr *);
  80. static void udf_load_fileset(struct super_block *, struct buffer_head *,
  81. struct kernel_lb_addr *);
  82. static void udf_open_lvid(struct super_block *);
  83. static void udf_close_lvid(struct super_block *);
  84. static unsigned int udf_count_free(struct super_block *);
  85. static int udf_statfs(struct dentry *, struct kstatfs *);
  86. static int udf_show_options(struct seq_file *, struct vfsmount *);
  87. static void udf_error(struct super_block *sb, const char *function,
  88. const char *fmt, ...);
  89. struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
  90. {
  91. struct logicalVolIntegrityDesc *lvid =
  92. (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
  93. __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
  94. __u32 offset = number_of_partitions * 2 *
  95. sizeof(uint32_t)/sizeof(uint8_t);
  96. return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
  97. }
  98. /* UDF filesystem type */
  99. static struct dentry *udf_mount(struct file_system_type *fs_type,
  100. int flags, const char *dev_name, void *data)
  101. {
  102. return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
  103. }
  104. static struct file_system_type udf_fstype = {
  105. .owner = THIS_MODULE,
  106. .name = "udf",
  107. .mount = udf_mount,
  108. .kill_sb = kill_block_super,
  109. .fs_flags = FS_REQUIRES_DEV,
  110. };
  111. static struct kmem_cache *udf_inode_cachep;
  112. static struct inode *udf_alloc_inode(struct super_block *sb)
  113. {
  114. struct udf_inode_info *ei;
  115. ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
  116. if (!ei)
  117. return NULL;
  118. ei->i_unique = 0;
  119. ei->i_lenExtents = 0;
  120. ei->i_next_alloc_block = 0;
  121. ei->i_next_alloc_goal = 0;
  122. ei->i_strat4096 = 0;
  123. init_rwsem(&ei->i_data_sem);
  124. return &ei->vfs_inode;
  125. }
  126. static void udf_i_callback(struct rcu_head *head)
  127. {
  128. struct inode *inode = container_of(head, struct inode, i_rcu);
  129. INIT_LIST_HEAD(&inode->i_dentry);
  130. kmem_cache_free(udf_inode_cachep, UDF_I(inode));
  131. }
  132. static void udf_destroy_inode(struct inode *inode)
  133. {
  134. call_rcu(&inode->i_rcu, udf_i_callback);
  135. }
  136. static void init_once(void *foo)
  137. {
  138. struct udf_inode_info *ei = (struct udf_inode_info *)foo;
  139. ei->i_ext.i_data = NULL;
  140. inode_init_once(&ei->vfs_inode);
  141. }
  142. static int init_inodecache(void)
  143. {
  144. udf_inode_cachep = kmem_cache_create("udf_inode_cache",
  145. sizeof(struct udf_inode_info),
  146. 0, (SLAB_RECLAIM_ACCOUNT |
  147. SLAB_MEM_SPREAD),
  148. init_once);
  149. if (!udf_inode_cachep)
  150. return -ENOMEM;
  151. return 0;
  152. }
  153. static void destroy_inodecache(void)
  154. {
  155. kmem_cache_destroy(udf_inode_cachep);
  156. }
  157. /* Superblock operations */
  158. static const struct super_operations udf_sb_ops = {
  159. .alloc_inode = udf_alloc_inode,
  160. .destroy_inode = udf_destroy_inode,
  161. .write_inode = udf_write_inode,
  162. .evict_inode = udf_evict_inode,
  163. .put_super = udf_put_super,
  164. .sync_fs = udf_sync_fs,
  165. .statfs = udf_statfs,
  166. .remount_fs = udf_remount_fs,
  167. .show_options = udf_show_options,
  168. };
  169. struct udf_options {
  170. unsigned char novrs;
  171. unsigned int blocksize;
  172. unsigned int session;
  173. unsigned int lastblock;
  174. unsigned int anchor;
  175. unsigned int volume;
  176. unsigned short partition;
  177. unsigned int fileset;
  178. unsigned int rootdir;
  179. unsigned int flags;
  180. mode_t umask;
  181. gid_t gid;
  182. uid_t uid;
  183. mode_t fmode;
  184. mode_t dmode;
  185. struct nls_table *nls_map;
  186. };
  187. static int __init init_udf_fs(void)
  188. {
  189. int err;
  190. err = init_inodecache();
  191. if (err)
  192. goto out1;
  193. err = register_filesystem(&udf_fstype);
  194. if (err)
  195. goto out;
  196. return 0;
  197. out:
  198. destroy_inodecache();
  199. out1:
  200. return err;
  201. }
  202. static void __exit exit_udf_fs(void)
  203. {
  204. unregister_filesystem(&udf_fstype);
  205. destroy_inodecache();
  206. }
  207. module_init(init_udf_fs)
  208. module_exit(exit_udf_fs)
  209. static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
  210. {
  211. struct udf_sb_info *sbi = UDF_SB(sb);
  212. sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
  213. GFP_KERNEL);
  214. if (!sbi->s_partmaps) {
  215. udf_error(sb, __func__,
  216. "Unable to allocate space for %d partition maps",
  217. count);
  218. sbi->s_partitions = 0;
  219. return -ENOMEM;
  220. }
  221. sbi->s_partitions = count;
  222. return 0;
  223. }
  224. static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
  225. {
  226. struct super_block *sb = mnt->mnt_sb;
  227. struct udf_sb_info *sbi = UDF_SB(sb);
  228. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
  229. seq_puts(seq, ",nostrict");
  230. if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
  231. seq_printf(seq, ",bs=%lu", sb->s_blocksize);
  232. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
  233. seq_puts(seq, ",unhide");
  234. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
  235. seq_puts(seq, ",undelete");
  236. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
  237. seq_puts(seq, ",noadinicb");
  238. if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
  239. seq_puts(seq, ",shortad");
  240. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
  241. seq_puts(seq, ",uid=forget");
  242. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
  243. seq_puts(seq, ",uid=ignore");
  244. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
  245. seq_puts(seq, ",gid=forget");
  246. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
  247. seq_puts(seq, ",gid=ignore");
  248. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
  249. seq_printf(seq, ",uid=%u", sbi->s_uid);
  250. if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
  251. seq_printf(seq, ",gid=%u", sbi->s_gid);
  252. if (sbi->s_umask != 0)
  253. seq_printf(seq, ",umask=%o", sbi->s_umask);
  254. if (sbi->s_fmode != UDF_INVALID_MODE)
  255. seq_printf(seq, ",mode=%o", sbi->s_fmode);
  256. if (sbi->s_dmode != UDF_INVALID_MODE)
  257. seq_printf(seq, ",dmode=%o", sbi->s_dmode);
  258. if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
  259. seq_printf(seq, ",session=%u", sbi->s_session);
  260. if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
  261. seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
  262. if (sbi->s_anchor != 0)
  263. seq_printf(seq, ",anchor=%u", sbi->s_anchor);
  264. /*
  265. * volume, partition, fileset and rootdir seem to be ignored
  266. * currently
  267. */
  268. if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
  269. seq_puts(seq, ",utf8");
  270. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
  271. seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
  272. return 0;
  273. }
  274. /*
  275. * udf_parse_options
  276. *
  277. * PURPOSE
  278. * Parse mount options.
  279. *
  280. * DESCRIPTION
  281. * The following mount options are supported:
  282. *
  283. * gid= Set the default group.
  284. * umask= Set the default umask.
  285. * mode= Set the default file permissions.
  286. * dmode= Set the default directory permissions.
  287. * uid= Set the default user.
  288. * bs= Set the block size.
  289. * unhide Show otherwise hidden files.
  290. * undelete Show deleted files in lists.
  291. * adinicb Embed data in the inode (default)
  292. * noadinicb Don't embed data in the inode
  293. * shortad Use short ad's
  294. * longad Use long ad's (default)
  295. * nostrict Unset strict conformance
  296. * iocharset= Set the NLS character set
  297. *
  298. * The remaining are for debugging and disaster recovery:
  299. *
  300. * novrs Skip volume sequence recognition
  301. *
  302. * The following expect a offset from 0.
  303. *
  304. * session= Set the CDROM session (default= last session)
  305. * anchor= Override standard anchor location. (default= 256)
  306. * volume= Override the VolumeDesc location. (unused)
  307. * partition= Override the PartitionDesc location. (unused)
  308. * lastblock= Set the last block of the filesystem/
  309. *
  310. * The following expect a offset from the partition root.
  311. *
  312. * fileset= Override the fileset block location. (unused)
  313. * rootdir= Override the root directory location. (unused)
  314. * WARNING: overriding the rootdir to a non-directory may
  315. * yield highly unpredictable results.
  316. *
  317. * PRE-CONDITIONS
  318. * options Pointer to mount options string.
  319. * uopts Pointer to mount options variable.
  320. *
  321. * POST-CONDITIONS
  322. * <return> 1 Mount options parsed okay.
  323. * <return> 0 Error parsing mount options.
  324. *
  325. * HISTORY
  326. * July 1, 1997 - Andrew E. Mileski
  327. * Written, tested, and released.
  328. */
  329. enum {
  330. Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
  331. Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
  332. Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
  333. Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
  334. Opt_rootdir, Opt_utf8, Opt_iocharset,
  335. Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
  336. Opt_fmode, Opt_dmode
  337. };
  338. static const match_table_t tokens = {
  339. {Opt_novrs, "novrs"},
  340. {Opt_nostrict, "nostrict"},
  341. {Opt_bs, "bs=%u"},
  342. {Opt_unhide, "unhide"},
  343. {Opt_undelete, "undelete"},
  344. {Opt_noadinicb, "noadinicb"},
  345. {Opt_adinicb, "adinicb"},
  346. {Opt_shortad, "shortad"},
  347. {Opt_longad, "longad"},
  348. {Opt_uforget, "uid=forget"},
  349. {Opt_uignore, "uid=ignore"},
  350. {Opt_gforget, "gid=forget"},
  351. {Opt_gignore, "gid=ignore"},
  352. {Opt_gid, "gid=%u"},
  353. {Opt_uid, "uid=%u"},
  354. {Opt_umask, "umask=%o"},
  355. {Opt_session, "session=%u"},
  356. {Opt_lastblock, "lastblock=%u"},
  357. {Opt_anchor, "anchor=%u"},
  358. {Opt_volume, "volume=%u"},
  359. {Opt_partition, "partition=%u"},
  360. {Opt_fileset, "fileset=%u"},
  361. {Opt_rootdir, "rootdir=%u"},
  362. {Opt_utf8, "utf8"},
  363. {Opt_iocharset, "iocharset=%s"},
  364. {Opt_fmode, "mode=%o"},
  365. {Opt_dmode, "dmode=%o"},
  366. {Opt_err, NULL}
  367. };
  368. static int udf_parse_options(char *options, struct udf_options *uopt,
  369. bool remount)
  370. {
  371. char *p;
  372. int option;
  373. uopt->novrs = 0;
  374. uopt->partition = 0xFFFF;
  375. uopt->session = 0xFFFFFFFF;
  376. uopt->lastblock = 0;
  377. uopt->anchor = 0;
  378. uopt->volume = 0xFFFFFFFF;
  379. uopt->rootdir = 0xFFFFFFFF;
  380. uopt->fileset = 0xFFFFFFFF;
  381. uopt->nls_map = NULL;
  382. if (!options)
  383. return 1;
  384. while ((p = strsep(&options, ",")) != NULL) {
  385. substring_t args[MAX_OPT_ARGS];
  386. int token;
  387. if (!*p)
  388. continue;
  389. token = match_token(p, tokens, args);
  390. switch (token) {
  391. case Opt_novrs:
  392. uopt->novrs = 1;
  393. break;
  394. case Opt_bs:
  395. if (match_int(&args[0], &option))
  396. return 0;
  397. uopt->blocksize = option;
  398. uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
  399. break;
  400. case Opt_unhide:
  401. uopt->flags |= (1 << UDF_FLAG_UNHIDE);
  402. break;
  403. case Opt_undelete:
  404. uopt->flags |= (1 << UDF_FLAG_UNDELETE);
  405. break;
  406. case Opt_noadinicb:
  407. uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
  408. break;
  409. case Opt_adinicb:
  410. uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
  411. break;
  412. case Opt_shortad:
  413. uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
  414. break;
  415. case Opt_longad:
  416. uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
  417. break;
  418. case Opt_gid:
  419. if (match_int(args, &option))
  420. return 0;
  421. uopt->gid = option;
  422. uopt->flags |= (1 << UDF_FLAG_GID_SET);
  423. break;
  424. case Opt_uid:
  425. if (match_int(args, &option))
  426. return 0;
  427. uopt->uid = option;
  428. uopt->flags |= (1 << UDF_FLAG_UID_SET);
  429. break;
  430. case Opt_umask:
  431. if (match_octal(args, &option))
  432. return 0;
  433. uopt->umask = option;
  434. break;
  435. case Opt_nostrict:
  436. uopt->flags &= ~(1 << UDF_FLAG_STRICT);
  437. break;
  438. case Opt_session:
  439. if (match_int(args, &option))
  440. return 0;
  441. uopt->session = option;
  442. if (!remount)
  443. uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
  444. break;
  445. case Opt_lastblock:
  446. if (match_int(args, &option))
  447. return 0;
  448. uopt->lastblock = option;
  449. if (!remount)
  450. uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
  451. break;
  452. case Opt_anchor:
  453. if (match_int(args, &option))
  454. return 0;
  455. uopt->anchor = option;
  456. break;
  457. case Opt_volume:
  458. if (match_int(args, &option))
  459. return 0;
  460. uopt->volume = option;
  461. break;
  462. case Opt_partition:
  463. if (match_int(args, &option))
  464. return 0;
  465. uopt->partition = option;
  466. break;
  467. case Opt_fileset:
  468. if (match_int(args, &option))
  469. return 0;
  470. uopt->fileset = option;
  471. break;
  472. case Opt_rootdir:
  473. if (match_int(args, &option))
  474. return 0;
  475. uopt->rootdir = option;
  476. break;
  477. case Opt_utf8:
  478. uopt->flags |= (1 << UDF_FLAG_UTF8);
  479. break;
  480. #ifdef CONFIG_UDF_NLS
  481. case Opt_iocharset:
  482. uopt->nls_map = load_nls(args[0].from);
  483. uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
  484. break;
  485. #endif
  486. case Opt_uignore:
  487. uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
  488. break;
  489. case Opt_uforget:
  490. uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
  491. break;
  492. case Opt_gignore:
  493. uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
  494. break;
  495. case Opt_gforget:
  496. uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
  497. break;
  498. case Opt_fmode:
  499. if (match_octal(args, &option))
  500. return 0;
  501. uopt->fmode = option & 0777;
  502. break;
  503. case Opt_dmode:
  504. if (match_octal(args, &option))
  505. return 0;
  506. uopt->dmode = option & 0777;
  507. break;
  508. default:
  509. printk(KERN_ERR "udf: bad mount option \"%s\" "
  510. "or missing value\n", p);
  511. return 0;
  512. }
  513. }
  514. return 1;
  515. }
  516. static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
  517. {
  518. struct udf_options uopt;
  519. struct udf_sb_info *sbi = UDF_SB(sb);
  520. int error = 0;
  521. uopt.flags = sbi->s_flags;
  522. uopt.uid = sbi->s_uid;
  523. uopt.gid = sbi->s_gid;
  524. uopt.umask = sbi->s_umask;
  525. uopt.fmode = sbi->s_fmode;
  526. uopt.dmode = sbi->s_dmode;
  527. if (!udf_parse_options(options, &uopt, true))
  528. return -EINVAL;
  529. write_lock(&sbi->s_cred_lock);
  530. sbi->s_flags = uopt.flags;
  531. sbi->s_uid = uopt.uid;
  532. sbi->s_gid = uopt.gid;
  533. sbi->s_umask = uopt.umask;
  534. sbi->s_fmode = uopt.fmode;
  535. sbi->s_dmode = uopt.dmode;
  536. write_unlock(&sbi->s_cred_lock);
  537. if (sbi->s_lvid_bh) {
  538. int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
  539. if (write_rev > UDF_MAX_WRITE_VERSION)
  540. *flags |= MS_RDONLY;
  541. }
  542. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  543. goto out_unlock;
  544. if (*flags & MS_RDONLY)
  545. udf_close_lvid(sb);
  546. else
  547. udf_open_lvid(sb);
  548. out_unlock:
  549. return error;
  550. }
  551. /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
  552. /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
  553. static loff_t udf_check_vsd(struct super_block *sb)
  554. {
  555. struct volStructDesc *vsd = NULL;
  556. loff_t sector = 32768;
  557. int sectorsize;
  558. struct buffer_head *bh = NULL;
  559. int nsr02 = 0;
  560. int nsr03 = 0;
  561. struct udf_sb_info *sbi;
  562. sbi = UDF_SB(sb);
  563. if (sb->s_blocksize < sizeof(struct volStructDesc))
  564. sectorsize = sizeof(struct volStructDesc);
  565. else
  566. sectorsize = sb->s_blocksize;
  567. sector += (sbi->s_session << sb->s_blocksize_bits);
  568. udf_debug("Starting at sector %u (%ld byte sectors)\n",
  569. (unsigned int)(sector >> sb->s_blocksize_bits),
  570. sb->s_blocksize);
  571. /* Process the sequence (if applicable) */
  572. for (; !nsr02 && !nsr03; sector += sectorsize) {
  573. /* Read a block */
  574. bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
  575. if (!bh)
  576. break;
  577. /* Look for ISO descriptors */
  578. vsd = (struct volStructDesc *)(bh->b_data +
  579. (sector & (sb->s_blocksize - 1)));
  580. if (vsd->stdIdent[0] == 0) {
  581. brelse(bh);
  582. break;
  583. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
  584. VSD_STD_ID_LEN)) {
  585. switch (vsd->structType) {
  586. case 0:
  587. udf_debug("ISO9660 Boot Record found\n");
  588. break;
  589. case 1:
  590. udf_debug("ISO9660 Primary Volume Descriptor "
  591. "found\n");
  592. break;
  593. case 2:
  594. udf_debug("ISO9660 Supplementary Volume "
  595. "Descriptor found\n");
  596. break;
  597. case 3:
  598. udf_debug("ISO9660 Volume Partition Descriptor "
  599. "found\n");
  600. break;
  601. case 255:
  602. udf_debug("ISO9660 Volume Descriptor Set "
  603. "Terminator found\n");
  604. break;
  605. default:
  606. udf_debug("ISO9660 VRS (%u) found\n",
  607. vsd->structType);
  608. break;
  609. }
  610. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
  611. VSD_STD_ID_LEN))
  612. ; /* nothing */
  613. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
  614. VSD_STD_ID_LEN)) {
  615. brelse(bh);
  616. break;
  617. } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
  618. VSD_STD_ID_LEN))
  619. nsr02 = sector;
  620. else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
  621. VSD_STD_ID_LEN))
  622. nsr03 = sector;
  623. brelse(bh);
  624. }
  625. if (nsr03)
  626. return nsr03;
  627. else if (nsr02)
  628. return nsr02;
  629. else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
  630. return -1;
  631. else
  632. return 0;
  633. }
  634. static int udf_find_fileset(struct super_block *sb,
  635. struct kernel_lb_addr *fileset,
  636. struct kernel_lb_addr *root)
  637. {
  638. struct buffer_head *bh = NULL;
  639. long lastblock;
  640. uint16_t ident;
  641. struct udf_sb_info *sbi;
  642. if (fileset->logicalBlockNum != 0xFFFFFFFF ||
  643. fileset->partitionReferenceNum != 0xFFFF) {
  644. bh = udf_read_ptagged(sb, fileset, 0, &ident);
  645. if (!bh) {
  646. return 1;
  647. } else if (ident != TAG_IDENT_FSD) {
  648. brelse(bh);
  649. return 1;
  650. }
  651. }
  652. sbi = UDF_SB(sb);
  653. if (!bh) {
  654. /* Search backwards through the partitions */
  655. struct kernel_lb_addr newfileset;
  656. /* --> cvg: FIXME - is it reasonable? */
  657. return 1;
  658. for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
  659. (newfileset.partitionReferenceNum != 0xFFFF &&
  660. fileset->logicalBlockNum == 0xFFFFFFFF &&
  661. fileset->partitionReferenceNum == 0xFFFF);
  662. newfileset.partitionReferenceNum--) {
  663. lastblock = sbi->s_partmaps
  664. [newfileset.partitionReferenceNum]
  665. .s_partition_len;
  666. newfileset.logicalBlockNum = 0;
  667. do {
  668. bh = udf_read_ptagged(sb, &newfileset, 0,
  669. &ident);
  670. if (!bh) {
  671. newfileset.logicalBlockNum++;
  672. continue;
  673. }
  674. switch (ident) {
  675. case TAG_IDENT_SBD:
  676. {
  677. struct spaceBitmapDesc *sp;
  678. sp = (struct spaceBitmapDesc *)
  679. bh->b_data;
  680. newfileset.logicalBlockNum += 1 +
  681. ((le32_to_cpu(sp->numOfBytes) +
  682. sizeof(struct spaceBitmapDesc)
  683. - 1) >> sb->s_blocksize_bits);
  684. brelse(bh);
  685. break;
  686. }
  687. case TAG_IDENT_FSD:
  688. *fileset = newfileset;
  689. break;
  690. default:
  691. newfileset.logicalBlockNum++;
  692. brelse(bh);
  693. bh = NULL;
  694. break;
  695. }
  696. } while (newfileset.logicalBlockNum < lastblock &&
  697. fileset->logicalBlockNum == 0xFFFFFFFF &&
  698. fileset->partitionReferenceNum == 0xFFFF);
  699. }
  700. }
  701. if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
  702. fileset->partitionReferenceNum != 0xFFFF) && bh) {
  703. udf_debug("Fileset at block=%d, partition=%d\n",
  704. fileset->logicalBlockNum,
  705. fileset->partitionReferenceNum);
  706. sbi->s_partition = fileset->partitionReferenceNum;
  707. udf_load_fileset(sb, bh, root);
  708. brelse(bh);
  709. return 0;
  710. }
  711. return 1;
  712. }
  713. static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
  714. {
  715. struct primaryVolDesc *pvoldesc;
  716. struct ustr *instr, *outstr;
  717. struct buffer_head *bh;
  718. uint16_t ident;
  719. int ret = 1;
  720. instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  721. if (!instr)
  722. return 1;
  723. outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
  724. if (!outstr)
  725. goto out1;
  726. bh = udf_read_tagged(sb, block, block, &ident);
  727. if (!bh)
  728. goto out2;
  729. BUG_ON(ident != TAG_IDENT_PVD);
  730. pvoldesc = (struct primaryVolDesc *)bh->b_data;
  731. if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
  732. pvoldesc->recordingDateAndTime)) {
  733. #ifdef UDFFS_DEBUG
  734. struct timestamp *ts = &pvoldesc->recordingDateAndTime;
  735. udf_debug("recording time %04u/%02u/%02u"
  736. " %02u:%02u (%x)\n",
  737. le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
  738. ts->minute, le16_to_cpu(ts->typeAndTimezone));
  739. #endif
  740. }
  741. if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
  742. if (udf_CS0toUTF8(outstr, instr)) {
  743. strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
  744. outstr->u_len > 31 ? 31 : outstr->u_len);
  745. udf_debug("volIdent[] = '%s'\n",
  746. UDF_SB(sb)->s_volume_ident);
  747. }
  748. if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
  749. if (udf_CS0toUTF8(outstr, instr))
  750. udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
  751. brelse(bh);
  752. ret = 0;
  753. out2:
  754. kfree(outstr);
  755. out1:
  756. kfree(instr);
  757. return ret;
  758. }
  759. static int udf_load_metadata_files(struct super_block *sb, int partition)
  760. {
  761. struct udf_sb_info *sbi = UDF_SB(sb);
  762. struct udf_part_map *map;
  763. struct udf_meta_data *mdata;
  764. struct kernel_lb_addr addr;
  765. int fe_error = 0;
  766. map = &sbi->s_partmaps[partition];
  767. mdata = &map->s_type_specific.s_metadata;
  768. /* metadata address */
  769. addr.logicalBlockNum = mdata->s_meta_file_loc;
  770. addr.partitionReferenceNum = map->s_partition_num;
  771. udf_debug("Metadata file location: block = %d part = %d\n",
  772. addr.logicalBlockNum, addr.partitionReferenceNum);
  773. mdata->s_metadata_fe = udf_iget(sb, &addr);
  774. if (mdata->s_metadata_fe == NULL) {
  775. udf_warning(sb, __func__, "metadata inode efe not found, "
  776. "will try mirror inode.");
  777. fe_error = 1;
  778. } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
  779. ICBTAG_FLAG_AD_SHORT) {
  780. udf_warning(sb, __func__, "metadata inode efe does not have "
  781. "short allocation descriptors!");
  782. fe_error = 1;
  783. iput(mdata->s_metadata_fe);
  784. mdata->s_metadata_fe = NULL;
  785. }
  786. /* mirror file entry */
  787. addr.logicalBlockNum = mdata->s_mirror_file_loc;
  788. addr.partitionReferenceNum = map->s_partition_num;
  789. udf_debug("Mirror metadata file location: block = %d part = %d\n",
  790. addr.logicalBlockNum, addr.partitionReferenceNum);
  791. mdata->s_mirror_fe = udf_iget(sb, &addr);
  792. if (mdata->s_mirror_fe == NULL) {
  793. if (fe_error) {
  794. udf_error(sb, __func__, "mirror inode efe not found "
  795. "and metadata inode is missing too, exiting...");
  796. goto error_exit;
  797. } else
  798. udf_warning(sb, __func__, "mirror inode efe not found,"
  799. " but metadata inode is OK");
  800. } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
  801. ICBTAG_FLAG_AD_SHORT) {
  802. udf_warning(sb, __func__, "mirror inode efe does not have "
  803. "short allocation descriptors!");
  804. iput(mdata->s_mirror_fe);
  805. mdata->s_mirror_fe = NULL;
  806. if (fe_error)
  807. goto error_exit;
  808. }
  809. /*
  810. * bitmap file entry
  811. * Note:
  812. * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
  813. */
  814. if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
  815. addr.logicalBlockNum = mdata->s_bitmap_file_loc;
  816. addr.partitionReferenceNum = map->s_partition_num;
  817. udf_debug("Bitmap file location: block = %d part = %d\n",
  818. addr.logicalBlockNum, addr.partitionReferenceNum);
  819. mdata->s_bitmap_fe = udf_iget(sb, &addr);
  820. if (mdata->s_bitmap_fe == NULL) {
  821. if (sb->s_flags & MS_RDONLY)
  822. udf_warning(sb, __func__, "bitmap inode efe "
  823. "not found but it's ok since the disc"
  824. " is mounted read-only");
  825. else {
  826. udf_error(sb, __func__, "bitmap inode efe not "
  827. "found and attempted read-write mount");
  828. goto error_exit;
  829. }
  830. }
  831. }
  832. udf_debug("udf_load_metadata_files Ok\n");
  833. return 0;
  834. error_exit:
  835. return 1;
  836. }
  837. static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
  838. struct kernel_lb_addr *root)
  839. {
  840. struct fileSetDesc *fset;
  841. fset = (struct fileSetDesc *)bh->b_data;
  842. *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
  843. UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
  844. udf_debug("Rootdir at block=%d, partition=%d\n",
  845. root->logicalBlockNum, root->partitionReferenceNum);
  846. }
  847. int udf_compute_nr_groups(struct super_block *sb, u32 partition)
  848. {
  849. struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
  850. return DIV_ROUND_UP(map->s_partition_len +
  851. (sizeof(struct spaceBitmapDesc) << 3),
  852. sb->s_blocksize * 8);
  853. }
  854. static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
  855. {
  856. struct udf_bitmap *bitmap;
  857. int nr_groups;
  858. int size;
  859. nr_groups = udf_compute_nr_groups(sb, index);
  860. size = sizeof(struct udf_bitmap) +
  861. (sizeof(struct buffer_head *) * nr_groups);
  862. if (size <= PAGE_SIZE)
  863. bitmap = kzalloc(size, GFP_KERNEL);
  864. else
  865. bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
  866. if (bitmap == NULL) {
  867. udf_error(sb, __func__,
  868. "Unable to allocate space for bitmap "
  869. "and %d buffer_head pointers", nr_groups);
  870. return NULL;
  871. }
  872. bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
  873. bitmap->s_nr_groups = nr_groups;
  874. return bitmap;
  875. }
  876. static int udf_fill_partdesc_info(struct super_block *sb,
  877. struct partitionDesc *p, int p_index)
  878. {
  879. struct udf_part_map *map;
  880. struct udf_sb_info *sbi = UDF_SB(sb);
  881. struct partitionHeaderDesc *phd;
  882. map = &sbi->s_partmaps[p_index];
  883. map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
  884. map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
  885. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
  886. map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
  887. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
  888. map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
  889. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
  890. map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
  891. if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
  892. map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
  893. udf_debug("Partition (%d type %x) starts at physical %d, "
  894. "block length %d\n", p_index,
  895. map->s_partition_type, map->s_partition_root,
  896. map->s_partition_len);
  897. if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
  898. strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
  899. return 0;
  900. phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
  901. if (phd->unallocSpaceTable.extLength) {
  902. struct kernel_lb_addr loc = {
  903. .logicalBlockNum = le32_to_cpu(
  904. phd->unallocSpaceTable.extPosition),
  905. .partitionReferenceNum = p_index,
  906. };
  907. map->s_uspace.s_table = udf_iget(sb, &loc);
  908. if (!map->s_uspace.s_table) {
  909. udf_debug("cannot load unallocSpaceTable (part %d)\n",
  910. p_index);
  911. return 1;
  912. }
  913. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
  914. udf_debug("unallocSpaceTable (part %d) @ %ld\n",
  915. p_index, map->s_uspace.s_table->i_ino);
  916. }
  917. if (phd->unallocSpaceBitmap.extLength) {
  918. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  919. if (!bitmap)
  920. return 1;
  921. map->s_uspace.s_bitmap = bitmap;
  922. bitmap->s_extLength = le32_to_cpu(
  923. phd->unallocSpaceBitmap.extLength);
  924. bitmap->s_extPosition = le32_to_cpu(
  925. phd->unallocSpaceBitmap.extPosition);
  926. map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
  927. udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
  928. bitmap->s_extPosition);
  929. }
  930. if (phd->partitionIntegrityTable.extLength)
  931. udf_debug("partitionIntegrityTable (part %d)\n", p_index);
  932. if (phd->freedSpaceTable.extLength) {
  933. struct kernel_lb_addr loc = {
  934. .logicalBlockNum = le32_to_cpu(
  935. phd->freedSpaceTable.extPosition),
  936. .partitionReferenceNum = p_index,
  937. };
  938. map->s_fspace.s_table = udf_iget(sb, &loc);
  939. if (!map->s_fspace.s_table) {
  940. udf_debug("cannot load freedSpaceTable (part %d)\n",
  941. p_index);
  942. return 1;
  943. }
  944. map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
  945. udf_debug("freedSpaceTable (part %d) @ %ld\n",
  946. p_index, map->s_fspace.s_table->i_ino);
  947. }
  948. if (phd->freedSpaceBitmap.extLength) {
  949. struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
  950. if (!bitmap)
  951. return 1;
  952. map->s_fspace.s_bitmap = bitmap;
  953. bitmap->s_extLength = le32_to_cpu(
  954. phd->freedSpaceBitmap.extLength);
  955. bitmap->s_extPosition = le32_to_cpu(
  956. phd->freedSpaceBitmap.extPosition);
  957. map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
  958. udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
  959. bitmap->s_extPosition);
  960. }
  961. return 0;
  962. }
  963. static void udf_find_vat_block(struct super_block *sb, int p_index,
  964. int type1_index, sector_t start_block)
  965. {
  966. struct udf_sb_info *sbi = UDF_SB(sb);
  967. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  968. sector_t vat_block;
  969. struct kernel_lb_addr ino;
  970. /*
  971. * VAT file entry is in the last recorded block. Some broken disks have
  972. * it a few blocks before so try a bit harder...
  973. */
  974. ino.partitionReferenceNum = type1_index;
  975. for (vat_block = start_block;
  976. vat_block >= map->s_partition_root &&
  977. vat_block >= start_block - 3 &&
  978. !sbi->s_vat_inode; vat_block--) {
  979. ino.logicalBlockNum = vat_block - map->s_partition_root;
  980. sbi->s_vat_inode = udf_iget(sb, &ino);
  981. }
  982. }
  983. static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
  984. {
  985. struct udf_sb_info *sbi = UDF_SB(sb);
  986. struct udf_part_map *map = &sbi->s_partmaps[p_index];
  987. struct buffer_head *bh = NULL;
  988. struct udf_inode_info *vati;
  989. uint32_t pos;
  990. struct virtualAllocationTable20 *vat20;
  991. sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  992. udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
  993. if (!sbi->s_vat_inode &&
  994. sbi->s_last_block != blocks - 1) {
  995. printk(KERN_NOTICE "UDF-fs: Failed to read VAT inode from the"
  996. " last recorded block (%lu), retrying with the last "
  997. "block of the device (%lu).\n",
  998. (unsigned long)sbi->s_last_block,
  999. (unsigned long)blocks - 1);
  1000. udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
  1001. }
  1002. if (!sbi->s_vat_inode)
  1003. return 1;
  1004. if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
  1005. map->s_type_specific.s_virtual.s_start_offset = 0;
  1006. map->s_type_specific.s_virtual.s_num_entries =
  1007. (sbi->s_vat_inode->i_size - 36) >> 2;
  1008. } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
  1009. vati = UDF_I(sbi->s_vat_inode);
  1010. if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
  1011. pos = udf_block_map(sbi->s_vat_inode, 0);
  1012. bh = sb_bread(sb, pos);
  1013. if (!bh)
  1014. return 1;
  1015. vat20 = (struct virtualAllocationTable20 *)bh->b_data;
  1016. } else {
  1017. vat20 = (struct virtualAllocationTable20 *)
  1018. vati->i_ext.i_data;
  1019. }
  1020. map->s_type_specific.s_virtual.s_start_offset =
  1021. le16_to_cpu(vat20->lengthHeader);
  1022. map->s_type_specific.s_virtual.s_num_entries =
  1023. (sbi->s_vat_inode->i_size -
  1024. map->s_type_specific.s_virtual.
  1025. s_start_offset) >> 2;
  1026. brelse(bh);
  1027. }
  1028. return 0;
  1029. }
  1030. static int udf_load_partdesc(struct super_block *sb, sector_t block)
  1031. {
  1032. struct buffer_head *bh;
  1033. struct partitionDesc *p;
  1034. struct udf_part_map *map;
  1035. struct udf_sb_info *sbi = UDF_SB(sb);
  1036. int i, type1_idx;
  1037. uint16_t partitionNumber;
  1038. uint16_t ident;
  1039. int ret = 0;
  1040. bh = udf_read_tagged(sb, block, block, &ident);
  1041. if (!bh)
  1042. return 1;
  1043. if (ident != TAG_IDENT_PD)
  1044. goto out_bh;
  1045. p = (struct partitionDesc *)bh->b_data;
  1046. partitionNumber = le16_to_cpu(p->partitionNumber);
  1047. /* First scan for TYPE1, SPARABLE and METADATA partitions */
  1048. for (i = 0; i < sbi->s_partitions; i++) {
  1049. map = &sbi->s_partmaps[i];
  1050. udf_debug("Searching map: (%d == %d)\n",
  1051. map->s_partition_num, partitionNumber);
  1052. if (map->s_partition_num == partitionNumber &&
  1053. (map->s_partition_type == UDF_TYPE1_MAP15 ||
  1054. map->s_partition_type == UDF_SPARABLE_MAP15))
  1055. break;
  1056. }
  1057. if (i >= sbi->s_partitions) {
  1058. udf_debug("Partition (%d) not found in partition map\n",
  1059. partitionNumber);
  1060. goto out_bh;
  1061. }
  1062. ret = udf_fill_partdesc_info(sb, p, i);
  1063. /*
  1064. * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
  1065. * PHYSICAL partitions are already set up
  1066. */
  1067. type1_idx = i;
  1068. for (i = 0; i < sbi->s_partitions; i++) {
  1069. map = &sbi->s_partmaps[i];
  1070. if (map->s_partition_num == partitionNumber &&
  1071. (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
  1072. map->s_partition_type == UDF_VIRTUAL_MAP20 ||
  1073. map->s_partition_type == UDF_METADATA_MAP25))
  1074. break;
  1075. }
  1076. if (i >= sbi->s_partitions)
  1077. goto out_bh;
  1078. ret = udf_fill_partdesc_info(sb, p, i);
  1079. if (ret)
  1080. goto out_bh;
  1081. if (map->s_partition_type == UDF_METADATA_MAP25) {
  1082. ret = udf_load_metadata_files(sb, i);
  1083. if (ret) {
  1084. printk(KERN_ERR "UDF-fs: error loading MetaData "
  1085. "partition map %d\n", i);
  1086. goto out_bh;
  1087. }
  1088. } else {
  1089. ret = udf_load_vat(sb, i, type1_idx);
  1090. if (ret)
  1091. goto out_bh;
  1092. /*
  1093. * Mark filesystem read-only if we have a partition with
  1094. * virtual map since we don't handle writing to it (we
  1095. * overwrite blocks instead of relocating them).
  1096. */
  1097. sb->s_flags |= MS_RDONLY;
  1098. printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
  1099. "because writing to pseudooverwrite partition is "
  1100. "not implemented.\n");
  1101. }
  1102. out_bh:
  1103. /* In case loading failed, we handle cleanup in udf_fill_super */
  1104. brelse(bh);
  1105. return ret;
  1106. }
  1107. static int udf_load_logicalvol(struct super_block *sb, sector_t block,
  1108. struct kernel_lb_addr *fileset)
  1109. {
  1110. struct logicalVolDesc *lvd;
  1111. int i, j, offset;
  1112. uint8_t type;
  1113. struct udf_sb_info *sbi = UDF_SB(sb);
  1114. struct genericPartitionMap *gpm;
  1115. uint16_t ident;
  1116. struct buffer_head *bh;
  1117. int ret = 0;
  1118. bh = udf_read_tagged(sb, block, block, &ident);
  1119. if (!bh)
  1120. return 1;
  1121. BUG_ON(ident != TAG_IDENT_LVD);
  1122. lvd = (struct logicalVolDesc *)bh->b_data;
  1123. i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
  1124. if (i != 0) {
  1125. ret = i;
  1126. goto out_bh;
  1127. }
  1128. for (i = 0, offset = 0;
  1129. i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
  1130. i++, offset += gpm->partitionMapLength) {
  1131. struct udf_part_map *map = &sbi->s_partmaps[i];
  1132. gpm = (struct genericPartitionMap *)
  1133. &(lvd->partitionMaps[offset]);
  1134. type = gpm->partitionMapType;
  1135. if (type == 1) {
  1136. struct genericPartitionMap1 *gpm1 =
  1137. (struct genericPartitionMap1 *)gpm;
  1138. map->s_partition_type = UDF_TYPE1_MAP15;
  1139. map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
  1140. map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
  1141. map->s_partition_func = NULL;
  1142. } else if (type == 2) {
  1143. struct udfPartitionMap2 *upm2 =
  1144. (struct udfPartitionMap2 *)gpm;
  1145. if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
  1146. strlen(UDF_ID_VIRTUAL))) {
  1147. u16 suf =
  1148. le16_to_cpu(((__le16 *)upm2->partIdent.
  1149. identSuffix)[0]);
  1150. if (suf < 0x0200) {
  1151. map->s_partition_type =
  1152. UDF_VIRTUAL_MAP15;
  1153. map->s_partition_func =
  1154. udf_get_pblock_virt15;
  1155. } else {
  1156. map->s_partition_type =
  1157. UDF_VIRTUAL_MAP20;
  1158. map->s_partition_func =
  1159. udf_get_pblock_virt20;
  1160. }
  1161. } else if (!strncmp(upm2->partIdent.ident,
  1162. UDF_ID_SPARABLE,
  1163. strlen(UDF_ID_SPARABLE))) {
  1164. uint32_t loc;
  1165. struct sparingTable *st;
  1166. struct sparablePartitionMap *spm =
  1167. (struct sparablePartitionMap *)gpm;
  1168. map->s_partition_type = UDF_SPARABLE_MAP15;
  1169. map->s_type_specific.s_sparing.s_packet_len =
  1170. le16_to_cpu(spm->packetLength);
  1171. for (j = 0; j < spm->numSparingTables; j++) {
  1172. struct buffer_head *bh2;
  1173. loc = le32_to_cpu(
  1174. spm->locSparingTable[j]);
  1175. bh2 = udf_read_tagged(sb, loc, loc,
  1176. &ident);
  1177. map->s_type_specific.s_sparing.
  1178. s_spar_map[j] = bh2;
  1179. if (bh2 == NULL)
  1180. continue;
  1181. st = (struct sparingTable *)bh2->b_data;
  1182. if (ident != 0 || strncmp(
  1183. st->sparingIdent.ident,
  1184. UDF_ID_SPARING,
  1185. strlen(UDF_ID_SPARING))) {
  1186. brelse(bh2);
  1187. map->s_type_specific.s_sparing.
  1188. s_spar_map[j] = NULL;
  1189. }
  1190. }
  1191. map->s_partition_func = udf_get_pblock_spar15;
  1192. } else if (!strncmp(upm2->partIdent.ident,
  1193. UDF_ID_METADATA,
  1194. strlen(UDF_ID_METADATA))) {
  1195. struct udf_meta_data *mdata =
  1196. &map->s_type_specific.s_metadata;
  1197. struct metadataPartitionMap *mdm =
  1198. (struct metadataPartitionMap *)
  1199. &(lvd->partitionMaps[offset]);
  1200. udf_debug("Parsing Logical vol part %d "
  1201. "type %d id=%s\n", i, type,
  1202. UDF_ID_METADATA);
  1203. map->s_partition_type = UDF_METADATA_MAP25;
  1204. map->s_partition_func = udf_get_pblock_meta25;
  1205. mdata->s_meta_file_loc =
  1206. le32_to_cpu(mdm->metadataFileLoc);
  1207. mdata->s_mirror_file_loc =
  1208. le32_to_cpu(mdm->metadataMirrorFileLoc);
  1209. mdata->s_bitmap_file_loc =
  1210. le32_to_cpu(mdm->metadataBitmapFileLoc);
  1211. mdata->s_alloc_unit_size =
  1212. le32_to_cpu(mdm->allocUnitSize);
  1213. mdata->s_align_unit_size =
  1214. le16_to_cpu(mdm->alignUnitSize);
  1215. mdata->s_dup_md_flag =
  1216. mdm->flags & 0x01;
  1217. udf_debug("Metadata Ident suffix=0x%x\n",
  1218. (le16_to_cpu(
  1219. ((__le16 *)
  1220. mdm->partIdent.identSuffix)[0])));
  1221. udf_debug("Metadata part num=%d\n",
  1222. le16_to_cpu(mdm->partitionNum));
  1223. udf_debug("Metadata part alloc unit size=%d\n",
  1224. le32_to_cpu(mdm->allocUnitSize));
  1225. udf_debug("Metadata file loc=%d\n",
  1226. le32_to_cpu(mdm->metadataFileLoc));
  1227. udf_debug("Mirror file loc=%d\n",
  1228. le32_to_cpu(mdm->metadataMirrorFileLoc));
  1229. udf_debug("Bitmap file loc=%d\n",
  1230. le32_to_cpu(mdm->metadataBitmapFileLoc));
  1231. udf_debug("Duplicate Flag: %d %d\n",
  1232. mdata->s_dup_md_flag, mdm->flags);
  1233. } else {
  1234. udf_debug("Unknown ident: %s\n",
  1235. upm2->partIdent.ident);
  1236. continue;
  1237. }
  1238. map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
  1239. map->s_partition_num = le16_to_cpu(upm2->partitionNum);
  1240. }
  1241. udf_debug("Partition (%d:%d) type %d on volume %d\n",
  1242. i, map->s_partition_num, type,
  1243. map->s_volumeseqnum);
  1244. }
  1245. if (fileset) {
  1246. struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
  1247. *fileset = lelb_to_cpu(la->extLocation);
  1248. udf_debug("FileSet found in LogicalVolDesc at block=%d, "
  1249. "partition=%d\n", fileset->logicalBlockNum,
  1250. fileset->partitionReferenceNum);
  1251. }
  1252. if (lvd->integritySeqExt.extLength)
  1253. udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
  1254. out_bh:
  1255. brelse(bh);
  1256. return ret;
  1257. }
  1258. /*
  1259. * udf_load_logicalvolint
  1260. *
  1261. */
  1262. static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
  1263. {
  1264. struct buffer_head *bh = NULL;
  1265. uint16_t ident;
  1266. struct udf_sb_info *sbi = UDF_SB(sb);
  1267. struct logicalVolIntegrityDesc *lvid;
  1268. while (loc.extLength > 0 &&
  1269. (bh = udf_read_tagged(sb, loc.extLocation,
  1270. loc.extLocation, &ident)) &&
  1271. ident == TAG_IDENT_LVID) {
  1272. sbi->s_lvid_bh = bh;
  1273. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1274. if (lvid->nextIntegrityExt.extLength)
  1275. udf_load_logicalvolint(sb,
  1276. leea_to_cpu(lvid->nextIntegrityExt));
  1277. if (sbi->s_lvid_bh != bh)
  1278. brelse(bh);
  1279. loc.extLength -= sb->s_blocksize;
  1280. loc.extLocation++;
  1281. }
  1282. if (sbi->s_lvid_bh != bh)
  1283. brelse(bh);
  1284. }
  1285. /*
  1286. * udf_process_sequence
  1287. *
  1288. * PURPOSE
  1289. * Process a main/reserve volume descriptor sequence.
  1290. *
  1291. * PRE-CONDITIONS
  1292. * sb Pointer to _locked_ superblock.
  1293. * block First block of first extent of the sequence.
  1294. * lastblock Lastblock of first extent of the sequence.
  1295. *
  1296. * HISTORY
  1297. * July 1, 1997 - Andrew E. Mileski
  1298. * Written, tested, and released.
  1299. */
  1300. static noinline int udf_process_sequence(struct super_block *sb, long block,
  1301. long lastblock, struct kernel_lb_addr *fileset)
  1302. {
  1303. struct buffer_head *bh = NULL;
  1304. struct udf_vds_record vds[VDS_POS_LENGTH];
  1305. struct udf_vds_record *curr;
  1306. struct generic_desc *gd;
  1307. struct volDescPtr *vdp;
  1308. int done = 0;
  1309. uint32_t vdsn;
  1310. uint16_t ident;
  1311. long next_s = 0, next_e = 0;
  1312. memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
  1313. /*
  1314. * Read the main descriptor sequence and find which descriptors
  1315. * are in it.
  1316. */
  1317. for (; (!done && block <= lastblock); block++) {
  1318. bh = udf_read_tagged(sb, block, block, &ident);
  1319. if (!bh) {
  1320. printk(KERN_ERR "udf: Block %Lu of volume descriptor "
  1321. "sequence is corrupted or we could not read "
  1322. "it.\n", (unsigned long long)block);
  1323. return 1;
  1324. }
  1325. /* Process each descriptor (ISO 13346 3/8.3-8.4) */
  1326. gd = (struct generic_desc *)bh->b_data;
  1327. vdsn = le32_to_cpu(gd->volDescSeqNum);
  1328. switch (ident) {
  1329. case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
  1330. curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
  1331. if (vdsn >= curr->volDescSeqNum) {
  1332. curr->volDescSeqNum = vdsn;
  1333. curr->block = block;
  1334. }
  1335. break;
  1336. case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
  1337. curr = &vds[VDS_POS_VOL_DESC_PTR];
  1338. if (vdsn >= curr->volDescSeqNum) {
  1339. curr->volDescSeqNum = vdsn;
  1340. curr->block = block;
  1341. vdp = (struct volDescPtr *)bh->b_data;
  1342. next_s = le32_to_cpu(
  1343. vdp->nextVolDescSeqExt.extLocation);
  1344. next_e = le32_to_cpu(
  1345. vdp->nextVolDescSeqExt.extLength);
  1346. next_e = next_e >> sb->s_blocksize_bits;
  1347. next_e += next_s;
  1348. }
  1349. break;
  1350. case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
  1351. curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
  1352. if (vdsn >= curr->volDescSeqNum) {
  1353. curr->volDescSeqNum = vdsn;
  1354. curr->block = block;
  1355. }
  1356. break;
  1357. case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
  1358. curr = &vds[VDS_POS_PARTITION_DESC];
  1359. if (!curr->block)
  1360. curr->block = block;
  1361. break;
  1362. case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
  1363. curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
  1364. if (vdsn >= curr->volDescSeqNum) {
  1365. curr->volDescSeqNum = vdsn;
  1366. curr->block = block;
  1367. }
  1368. break;
  1369. case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
  1370. curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
  1371. if (vdsn >= curr->volDescSeqNum) {
  1372. curr->volDescSeqNum = vdsn;
  1373. curr->block = block;
  1374. }
  1375. break;
  1376. case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
  1377. vds[VDS_POS_TERMINATING_DESC].block = block;
  1378. if (next_e) {
  1379. block = next_s;
  1380. lastblock = next_e;
  1381. next_s = next_e = 0;
  1382. } else
  1383. done = 1;
  1384. break;
  1385. }
  1386. brelse(bh);
  1387. }
  1388. /*
  1389. * Now read interesting descriptors again and process them
  1390. * in a suitable order
  1391. */
  1392. if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
  1393. printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
  1394. return 1;
  1395. }
  1396. if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
  1397. return 1;
  1398. if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
  1399. vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
  1400. return 1;
  1401. if (vds[VDS_POS_PARTITION_DESC].block) {
  1402. /*
  1403. * We rescan the whole descriptor sequence to find
  1404. * partition descriptor blocks and process them.
  1405. */
  1406. for (block = vds[VDS_POS_PARTITION_DESC].block;
  1407. block < vds[VDS_POS_TERMINATING_DESC].block;
  1408. block++)
  1409. if (udf_load_partdesc(sb, block))
  1410. return 1;
  1411. }
  1412. return 0;
  1413. }
  1414. static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
  1415. struct kernel_lb_addr *fileset)
  1416. {
  1417. struct anchorVolDescPtr *anchor;
  1418. long main_s, main_e, reserve_s, reserve_e;
  1419. anchor = (struct anchorVolDescPtr *)bh->b_data;
  1420. /* Locate the main sequence */
  1421. main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
  1422. main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
  1423. main_e = main_e >> sb->s_blocksize_bits;
  1424. main_e += main_s;
  1425. /* Locate the reserve sequence */
  1426. reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
  1427. reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
  1428. reserve_e = reserve_e >> sb->s_blocksize_bits;
  1429. reserve_e += reserve_s;
  1430. /* Process the main & reserve sequences */
  1431. /* responsible for finding the PartitionDesc(s) */
  1432. if (!udf_process_sequence(sb, main_s, main_e, fileset))
  1433. return 1;
  1434. return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
  1435. }
  1436. /*
  1437. * Check whether there is an anchor block in the given block and
  1438. * load Volume Descriptor Sequence if so.
  1439. */
  1440. static int udf_check_anchor_block(struct super_block *sb, sector_t block,
  1441. struct kernel_lb_addr *fileset)
  1442. {
  1443. struct buffer_head *bh;
  1444. uint16_t ident;
  1445. int ret;
  1446. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
  1447. udf_fixed_to_variable(block) >=
  1448. sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
  1449. return 0;
  1450. bh = udf_read_tagged(sb, block, block, &ident);
  1451. if (!bh)
  1452. return 0;
  1453. if (ident != TAG_IDENT_AVDP) {
  1454. brelse(bh);
  1455. return 0;
  1456. }
  1457. ret = udf_load_sequence(sb, bh, fileset);
  1458. brelse(bh);
  1459. return ret;
  1460. }
  1461. /* Search for an anchor volume descriptor pointer */
  1462. static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
  1463. struct kernel_lb_addr *fileset)
  1464. {
  1465. sector_t last[6];
  1466. int i;
  1467. struct udf_sb_info *sbi = UDF_SB(sb);
  1468. int last_count = 0;
  1469. /* First try user provided anchor */
  1470. if (sbi->s_anchor) {
  1471. if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
  1472. return lastblock;
  1473. }
  1474. /*
  1475. * according to spec, anchor is in either:
  1476. * block 256
  1477. * lastblock-256
  1478. * lastblock
  1479. * however, if the disc isn't closed, it could be 512.
  1480. */
  1481. if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
  1482. return lastblock;
  1483. /*
  1484. * The trouble is which block is the last one. Drives often misreport
  1485. * this so we try various possibilities.
  1486. */
  1487. last[last_count++] = lastblock;
  1488. if (lastblock >= 1)
  1489. last[last_count++] = lastblock - 1;
  1490. last[last_count++] = lastblock + 1;
  1491. if (lastblock >= 2)
  1492. last[last_count++] = lastblock - 2;
  1493. if (lastblock >= 150)
  1494. last[last_count++] = lastblock - 150;
  1495. if (lastblock >= 152)
  1496. last[last_count++] = lastblock - 152;
  1497. for (i = 0; i < last_count; i++) {
  1498. if (last[i] >= sb->s_bdev->bd_inode->i_size >>
  1499. sb->s_blocksize_bits)
  1500. continue;
  1501. if (udf_check_anchor_block(sb, last[i], fileset))
  1502. return last[i];
  1503. if (last[i] < 256)
  1504. continue;
  1505. if (udf_check_anchor_block(sb, last[i] - 256, fileset))
  1506. return last[i];
  1507. }
  1508. /* Finally try block 512 in case media is open */
  1509. if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
  1510. return last[0];
  1511. return 0;
  1512. }
  1513. /*
  1514. * Find an anchor volume descriptor and load Volume Descriptor Sequence from
  1515. * area specified by it. The function expects sbi->s_lastblock to be the last
  1516. * block on the media.
  1517. *
  1518. * Return 1 if ok, 0 if not found.
  1519. *
  1520. */
  1521. static int udf_find_anchor(struct super_block *sb,
  1522. struct kernel_lb_addr *fileset)
  1523. {
  1524. sector_t lastblock;
  1525. struct udf_sb_info *sbi = UDF_SB(sb);
  1526. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1527. if (lastblock)
  1528. goto out;
  1529. /* No anchor found? Try VARCONV conversion of block numbers */
  1530. UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
  1531. /* Firstly, we try to not convert number of the last block */
  1532. lastblock = udf_scan_anchors(sb,
  1533. udf_variable_to_fixed(sbi->s_last_block),
  1534. fileset);
  1535. if (lastblock)
  1536. goto out;
  1537. /* Secondly, we try with converted number of the last block */
  1538. lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
  1539. if (!lastblock) {
  1540. /* VARCONV didn't help. Clear it. */
  1541. UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
  1542. return 0;
  1543. }
  1544. out:
  1545. sbi->s_last_block = lastblock;
  1546. return 1;
  1547. }
  1548. /*
  1549. * Check Volume Structure Descriptor, find Anchor block and load Volume
  1550. * Descriptor Sequence
  1551. */
  1552. static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
  1553. int silent, struct kernel_lb_addr *fileset)
  1554. {
  1555. struct udf_sb_info *sbi = UDF_SB(sb);
  1556. loff_t nsr_off;
  1557. if (!sb_set_blocksize(sb, uopt->blocksize)) {
  1558. if (!silent)
  1559. printk(KERN_WARNING "UDF-fs: Bad block size\n");
  1560. return 0;
  1561. }
  1562. sbi->s_last_block = uopt->lastblock;
  1563. if (!uopt->novrs) {
  1564. /* Check that it is NSR02 compliant */
  1565. nsr_off = udf_check_vsd(sb);
  1566. if (!nsr_off) {
  1567. if (!silent)
  1568. printk(KERN_WARNING "UDF-fs: No VRS found\n");
  1569. return 0;
  1570. }
  1571. if (nsr_off == -1)
  1572. udf_debug("Failed to read byte 32768. Assuming open "
  1573. "disc. Skipping validity check\n");
  1574. if (!sbi->s_last_block)
  1575. sbi->s_last_block = udf_get_last_block(sb);
  1576. } else {
  1577. udf_debug("Validity check skipped because of novrs option\n");
  1578. }
  1579. /* Look for anchor block and load Volume Descriptor Sequence */
  1580. sbi->s_anchor = uopt->anchor;
  1581. if (!udf_find_anchor(sb, fileset)) {
  1582. if (!silent)
  1583. printk(KERN_WARNING "UDF-fs: No anchor found\n");
  1584. return 0;
  1585. }
  1586. return 1;
  1587. }
  1588. static void udf_open_lvid(struct super_block *sb)
  1589. {
  1590. struct udf_sb_info *sbi = UDF_SB(sb);
  1591. struct buffer_head *bh = sbi->s_lvid_bh;
  1592. struct logicalVolIntegrityDesc *lvid;
  1593. struct logicalVolIntegrityDescImpUse *lvidiu;
  1594. if (!bh)
  1595. return;
  1596. mutex_lock(&sbi->s_alloc_mutex);
  1597. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1598. lvidiu = udf_sb_lvidiu(sbi);
  1599. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1600. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1601. udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
  1602. CURRENT_TIME);
  1603. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
  1604. lvid->descTag.descCRC = cpu_to_le16(
  1605. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1606. le16_to_cpu(lvid->descTag.descCRCLength)));
  1607. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1608. mark_buffer_dirty(bh);
  1609. sbi->s_lvid_dirty = 0;
  1610. mutex_unlock(&sbi->s_alloc_mutex);
  1611. }
  1612. static void udf_close_lvid(struct super_block *sb)
  1613. {
  1614. struct udf_sb_info *sbi = UDF_SB(sb);
  1615. struct buffer_head *bh = sbi->s_lvid_bh;
  1616. struct logicalVolIntegrityDesc *lvid;
  1617. struct logicalVolIntegrityDescImpUse *lvidiu;
  1618. if (!bh)
  1619. return;
  1620. mutex_lock(&sbi->s_alloc_mutex);
  1621. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1622. lvidiu = udf_sb_lvidiu(sbi);
  1623. lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1624. lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1625. udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
  1626. if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
  1627. lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
  1628. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
  1629. lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
  1630. if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
  1631. lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
  1632. lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
  1633. lvid->descTag.descCRC = cpu_to_le16(
  1634. crc_itu_t(0, (char *)lvid + sizeof(struct tag),
  1635. le16_to_cpu(lvid->descTag.descCRCLength)));
  1636. lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
  1637. /*
  1638. * We set buffer uptodate unconditionally here to avoid spurious
  1639. * warnings from mark_buffer_dirty() when previous EIO has marked
  1640. * the buffer as !uptodate
  1641. */
  1642. set_buffer_uptodate(bh);
  1643. mark_buffer_dirty(bh);
  1644. sbi->s_lvid_dirty = 0;
  1645. mutex_unlock(&sbi->s_alloc_mutex);
  1646. }
  1647. u64 lvid_get_unique_id(struct super_block *sb)
  1648. {
  1649. struct buffer_head *bh;
  1650. struct udf_sb_info *sbi = UDF_SB(sb);
  1651. struct logicalVolIntegrityDesc *lvid;
  1652. struct logicalVolHeaderDesc *lvhd;
  1653. u64 uniqueID;
  1654. u64 ret;
  1655. bh = sbi->s_lvid_bh;
  1656. if (!bh)
  1657. return 0;
  1658. lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
  1659. lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
  1660. mutex_lock(&sbi->s_alloc_mutex);
  1661. ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
  1662. if (!(++uniqueID & 0xFFFFFFFF))
  1663. uniqueID += 16;
  1664. lvhd->uniqueID = cpu_to_le64(uniqueID);
  1665. mutex_unlock(&sbi->s_alloc_mutex);
  1666. mark_buffer_dirty(bh);
  1667. return ret;
  1668. }
  1669. static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
  1670. {
  1671. int i;
  1672. int nr_groups = bitmap->s_nr_groups;
  1673. int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
  1674. nr_groups);
  1675. for (i = 0; i < nr_groups; i++)
  1676. if (bitmap->s_block_bitmap[i])
  1677. brelse(bitmap->s_block_bitmap[i]);
  1678. if (size <= PAGE_SIZE)
  1679. kfree(bitmap);
  1680. else
  1681. vfree(bitmap);
  1682. }
  1683. static void udf_free_partition(struct udf_part_map *map)
  1684. {
  1685. int i;
  1686. struct udf_meta_data *mdata;
  1687. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
  1688. iput(map->s_uspace.s_table);
  1689. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
  1690. iput(map->s_fspace.s_table);
  1691. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
  1692. udf_sb_free_bitmap(map->s_uspace.s_bitmap);
  1693. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
  1694. udf_sb_free_bitmap(map->s_fspace.s_bitmap);
  1695. if (map->s_partition_type == UDF_SPARABLE_MAP15)
  1696. for (i = 0; i < 4; i++)
  1697. brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
  1698. else if (map->s_partition_type == UDF_METADATA_MAP25) {
  1699. mdata = &map->s_type_specific.s_metadata;
  1700. iput(mdata->s_metadata_fe);
  1701. mdata->s_metadata_fe = NULL;
  1702. iput(mdata->s_mirror_fe);
  1703. mdata->s_mirror_fe = NULL;
  1704. iput(mdata->s_bitmap_fe);
  1705. mdata->s_bitmap_fe = NULL;
  1706. }
  1707. }
  1708. static int udf_fill_super(struct super_block *sb, void *options, int silent)
  1709. {
  1710. int i;
  1711. int ret;
  1712. struct inode *inode = NULL;
  1713. struct udf_options uopt;
  1714. struct kernel_lb_addr rootdir, fileset;
  1715. struct udf_sb_info *sbi;
  1716. uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
  1717. uopt.uid = -1;
  1718. uopt.gid = -1;
  1719. uopt.umask = 0;
  1720. uopt.fmode = UDF_INVALID_MODE;
  1721. uopt.dmode = UDF_INVALID_MODE;
  1722. sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
  1723. if (!sbi)
  1724. return -ENOMEM;
  1725. sb->s_fs_info = sbi;
  1726. mutex_init(&sbi->s_alloc_mutex);
  1727. if (!udf_parse_options((char *)options, &uopt, false))
  1728. goto error_out;
  1729. if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
  1730. uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
  1731. udf_error(sb, "udf_read_super",
  1732. "utf8 cannot be combined with iocharset\n");
  1733. goto error_out;
  1734. }
  1735. #ifdef CONFIG_UDF_NLS
  1736. if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
  1737. uopt.nls_map = load_nls_default();
  1738. if (!uopt.nls_map)
  1739. uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
  1740. else
  1741. udf_debug("Using default NLS map\n");
  1742. }
  1743. #endif
  1744. if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
  1745. uopt.flags |= (1 << UDF_FLAG_UTF8);
  1746. fileset.logicalBlockNum = 0xFFFFFFFF;
  1747. fileset.partitionReferenceNum = 0xFFFF;
  1748. sbi->s_flags = uopt.flags;
  1749. sbi->s_uid = uopt.uid;
  1750. sbi->s_gid = uopt.gid;
  1751. sbi->s_umask = uopt.umask;
  1752. sbi->s_fmode = uopt.fmode;
  1753. sbi->s_dmode = uopt.dmode;
  1754. sbi->s_nls_map = uopt.nls_map;
  1755. rwlock_init(&sbi->s_cred_lock);
  1756. if (uopt.session == 0xFFFFFFFF)
  1757. sbi->s_session = udf_get_last_session(sb);
  1758. else
  1759. sbi->s_session = uopt.session;
  1760. udf_debug("Multi-session=%d\n", sbi->s_session);
  1761. /* Fill in the rest of the superblock */
  1762. sb->s_op = &udf_sb_ops;
  1763. sb->s_export_op = &udf_export_ops;
  1764. sb->s_dirt = 0;
  1765. sb->s_magic = UDF_SUPER_MAGIC;
  1766. sb->s_time_gran = 1000;
  1767. if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
  1768. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1769. } else {
  1770. uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
  1771. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1772. if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
  1773. if (!silent)
  1774. printk(KERN_NOTICE
  1775. "UDF-fs: Rescanning with blocksize "
  1776. "%d\n", UDF_DEFAULT_BLOCKSIZE);
  1777. uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
  1778. ret = udf_load_vrs(sb, &uopt, silent, &fileset);
  1779. }
  1780. }
  1781. if (!ret) {
  1782. printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
  1783. goto error_out;
  1784. }
  1785. udf_debug("Lastblock=%d\n", sbi->s_last_block);
  1786. if (sbi->s_lvid_bh) {
  1787. struct logicalVolIntegrityDescImpUse *lvidiu =
  1788. udf_sb_lvidiu(sbi);
  1789. uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
  1790. uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
  1791. /* uint16_t maxUDFWriteRev =
  1792. le16_to_cpu(lvidiu->maxUDFWriteRev); */
  1793. if (minUDFReadRev > UDF_MAX_READ_VERSION) {
  1794. printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
  1795. "(max is %x)\n",
  1796. le16_to_cpu(lvidiu->minUDFReadRev),
  1797. UDF_MAX_READ_VERSION);
  1798. goto error_out;
  1799. } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
  1800. sb->s_flags |= MS_RDONLY;
  1801. sbi->s_udfrev = minUDFWriteRev;
  1802. if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
  1803. UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
  1804. if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
  1805. UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
  1806. }
  1807. if (!sbi->s_partitions) {
  1808. printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
  1809. goto error_out;
  1810. }
  1811. if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
  1812. UDF_PART_FLAG_READ_ONLY) {
  1813. printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
  1814. "forcing readonly mount\n");
  1815. sb->s_flags |= MS_RDONLY;
  1816. }
  1817. if (udf_find_fileset(sb, &fileset, &rootdir)) {
  1818. printk(KERN_WARNING "UDF-fs: No fileset found\n");
  1819. goto error_out;
  1820. }
  1821. if (!silent) {
  1822. struct timestamp ts;
  1823. udf_time_to_disk_stamp(&ts, sbi->s_record_time);
  1824. udf_info("UDF: Mounting volume '%s', "
  1825. "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
  1826. sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
  1827. ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
  1828. }
  1829. if (!(sb->s_flags & MS_RDONLY))
  1830. udf_open_lvid(sb);
  1831. /* Assign the root inode */
  1832. /* assign inodes by physical block number */
  1833. /* perhaps it's not extensible enough, but for now ... */
  1834. inode = udf_iget(sb, &rootdir);
  1835. if (!inode) {
  1836. printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
  1837. "partition=%d\n",
  1838. rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
  1839. goto error_out;
  1840. }
  1841. /* Allocate a dentry for the root inode */
  1842. sb->s_root = d_alloc_root(inode);
  1843. if (!sb->s_root) {
  1844. printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
  1845. iput(inode);
  1846. goto error_out;
  1847. }
  1848. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1849. return 0;
  1850. error_out:
  1851. if (sbi->s_vat_inode)
  1852. iput(sbi->s_vat_inode);
  1853. if (sbi->s_partitions)
  1854. for (i = 0; i < sbi->s_partitions; i++)
  1855. udf_free_partition(&sbi->s_partmaps[i]);
  1856. #ifdef CONFIG_UDF_NLS
  1857. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1858. unload_nls(sbi->s_nls_map);
  1859. #endif
  1860. if (!(sb->s_flags & MS_RDONLY))
  1861. udf_close_lvid(sb);
  1862. brelse(sbi->s_lvid_bh);
  1863. kfree(sbi->s_partmaps);
  1864. kfree(sbi);
  1865. sb->s_fs_info = NULL;
  1866. return -EINVAL;
  1867. }
  1868. static void udf_error(struct super_block *sb, const char *function,
  1869. const char *fmt, ...)
  1870. {
  1871. va_list args;
  1872. if (!(sb->s_flags & MS_RDONLY)) {
  1873. /* mark sb error */
  1874. sb->s_dirt = 1;
  1875. }
  1876. va_start(args, fmt);
  1877. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1878. va_end(args);
  1879. printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
  1880. sb->s_id, function, error_buf);
  1881. }
  1882. void udf_warning(struct super_block *sb, const char *function,
  1883. const char *fmt, ...)
  1884. {
  1885. va_list args;
  1886. va_start(args, fmt);
  1887. vsnprintf(error_buf, sizeof(error_buf), fmt, args);
  1888. va_end(args);
  1889. printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
  1890. sb->s_id, function, error_buf);
  1891. }
  1892. static void udf_put_super(struct super_block *sb)
  1893. {
  1894. int i;
  1895. struct udf_sb_info *sbi;
  1896. sbi = UDF_SB(sb);
  1897. if (sbi->s_vat_inode)
  1898. iput(sbi->s_vat_inode);
  1899. if (sbi->s_partitions)
  1900. for (i = 0; i < sbi->s_partitions; i++)
  1901. udf_free_partition(&sbi->s_partmaps[i]);
  1902. #ifdef CONFIG_UDF_NLS
  1903. if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
  1904. unload_nls(sbi->s_nls_map);
  1905. #endif
  1906. if (!(sb->s_flags & MS_RDONLY))
  1907. udf_close_lvid(sb);
  1908. brelse(sbi->s_lvid_bh);
  1909. kfree(sbi->s_partmaps);
  1910. kfree(sb->s_fs_info);
  1911. sb->s_fs_info = NULL;
  1912. }
  1913. static int udf_sync_fs(struct super_block *sb, int wait)
  1914. {
  1915. struct udf_sb_info *sbi = UDF_SB(sb);
  1916. mutex_lock(&sbi->s_alloc_mutex);
  1917. if (sbi->s_lvid_dirty) {
  1918. /*
  1919. * Blockdevice will be synced later so we don't have to submit
  1920. * the buffer for IO
  1921. */
  1922. mark_buffer_dirty(sbi->s_lvid_bh);
  1923. sb->s_dirt = 0;
  1924. sbi->s_lvid_dirty = 0;
  1925. }
  1926. mutex_unlock(&sbi->s_alloc_mutex);
  1927. return 0;
  1928. }
  1929. static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
  1930. {
  1931. struct super_block *sb = dentry->d_sb;
  1932. struct udf_sb_info *sbi = UDF_SB(sb);
  1933. struct logicalVolIntegrityDescImpUse *lvidiu;
  1934. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1935. if (sbi->s_lvid_bh != NULL)
  1936. lvidiu = udf_sb_lvidiu(sbi);
  1937. else
  1938. lvidiu = NULL;
  1939. buf->f_type = UDF_SUPER_MAGIC;
  1940. buf->f_bsize = sb->s_blocksize;
  1941. buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
  1942. buf->f_bfree = udf_count_free(sb);
  1943. buf->f_bavail = buf->f_bfree;
  1944. buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
  1945. le32_to_cpu(lvidiu->numDirs)) : 0)
  1946. + buf->f_bfree;
  1947. buf->f_ffree = buf->f_bfree;
  1948. buf->f_namelen = UDF_NAME_LEN - 2;
  1949. buf->f_fsid.val[0] = (u32)id;
  1950. buf->f_fsid.val[1] = (u32)(id >> 32);
  1951. return 0;
  1952. }
  1953. static unsigned int udf_count_free_bitmap(struct super_block *sb,
  1954. struct udf_bitmap *bitmap)
  1955. {
  1956. struct buffer_head *bh = NULL;
  1957. unsigned int accum = 0;
  1958. int index;
  1959. int block = 0, newblock;
  1960. struct kernel_lb_addr loc;
  1961. uint32_t bytes;
  1962. uint8_t *ptr;
  1963. uint16_t ident;
  1964. struct spaceBitmapDesc *bm;
  1965. loc.logicalBlockNum = bitmap->s_extPosition;
  1966. loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
  1967. bh = udf_read_ptagged(sb, &loc, 0, &ident);
  1968. if (!bh) {
  1969. printk(KERN_ERR "udf: udf_count_free failed\n");
  1970. goto out;
  1971. } else if (ident != TAG_IDENT_SBD) {
  1972. brelse(bh);
  1973. printk(KERN_ERR "udf: udf_count_free failed\n");
  1974. goto out;
  1975. }
  1976. bm = (struct spaceBitmapDesc *)bh->b_data;
  1977. bytes = le32_to_cpu(bm->numOfBytes);
  1978. index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
  1979. ptr = (uint8_t *)bh->b_data;
  1980. while (bytes > 0) {
  1981. u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
  1982. accum += bitmap_weight((const unsigned long *)(ptr + index),
  1983. cur_bytes * 8);
  1984. bytes -= cur_bytes;
  1985. if (bytes) {
  1986. brelse(bh);
  1987. newblock = udf_get_lb_pblock(sb, &loc, ++block);
  1988. bh = udf_tread(sb, newblock);
  1989. if (!bh) {
  1990. udf_debug("read failed\n");
  1991. goto out;
  1992. }
  1993. index = 0;
  1994. ptr = (uint8_t *)bh->b_data;
  1995. }
  1996. }
  1997. brelse(bh);
  1998. out:
  1999. return accum;
  2000. }
  2001. static unsigned int udf_count_free_table(struct super_block *sb,
  2002. struct inode *table)
  2003. {
  2004. unsigned int accum = 0;
  2005. uint32_t elen;
  2006. struct kernel_lb_addr eloc;
  2007. int8_t etype;
  2008. struct extent_position epos;
  2009. mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
  2010. epos.block = UDF_I(table)->i_location;
  2011. epos.offset = sizeof(struct unallocSpaceEntry);
  2012. epos.bh = NULL;
  2013. while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
  2014. accum += (elen >> table->i_sb->s_blocksize_bits);
  2015. brelse(epos.bh);
  2016. mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
  2017. return accum;
  2018. }
  2019. static unsigned int udf_count_free(struct super_block *sb)
  2020. {
  2021. unsigned int accum = 0;
  2022. struct udf_sb_info *sbi;
  2023. struct udf_part_map *map;
  2024. sbi = UDF_SB(sb);
  2025. if (sbi->s_lvid_bh) {
  2026. struct logicalVolIntegrityDesc *lvid =
  2027. (struct logicalVolIntegrityDesc *)
  2028. sbi->s_lvid_bh->b_data;
  2029. if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
  2030. accum = le32_to_cpu(
  2031. lvid->freeSpaceTable[sbi->s_partition]);
  2032. if (accum == 0xFFFFFFFF)
  2033. accum = 0;
  2034. }
  2035. }
  2036. if (accum)
  2037. return accum;
  2038. map = &sbi->s_partmaps[sbi->s_partition];
  2039. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
  2040. accum += udf_count_free_bitmap(sb,
  2041. map->s_uspace.s_bitmap);
  2042. }
  2043. if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
  2044. accum += udf_count_free_bitmap(sb,
  2045. map->s_fspace.s_bitmap);
  2046. }
  2047. if (accum)
  2048. return accum;
  2049. if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
  2050. accum += udf_count_free_table(sb,
  2051. map->s_uspace.s_table);
  2052. }
  2053. if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
  2054. accum += udf_count_free_table(sb,
  2055. map->s_fspace.s_table);
  2056. }
  2057. return accum;
  2058. }