inode.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  1. /*
  2. * inode.c
  3. *
  4. * PURPOSE
  5. * Inode handling routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 10/04/98 dgb Added rudimentary directory functions
  20. * 10/07/98 Fully working udf_block_map! It works!
  21. * 11/25/98 bmap altered to better support extents
  22. * 12/06/98 blf partition support in udf_iget, udf_block_map
  23. * and udf_read_inode
  24. * 12/12/98 rewrote udf_block_map to handle next extents and descs across
  25. * block boundaries (which is not actually allowed)
  26. * 12/20/98 added support for strategy 4096
  27. * 03/07/99 rewrote udf_block_map (again)
  28. * New funcs, inode_bmap, udf_next_aext
  29. * 04/19/99 Support for writing device EA's for major/minor #
  30. */
  31. #include "udfdecl.h"
  32. #include <linux/mm.h>
  33. #include <linux/smp_lock.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/buffer_head.h>
  37. #include <linux/writeback.h>
  38. #include <linux/slab.h>
  39. #include <linux/crc-itu-t.h>
  40. #include "udf_i.h"
  41. #include "udf_sb.h"
  42. MODULE_AUTHOR("Ben Fennema");
  43. MODULE_DESCRIPTION("Universal Disk Format Filesystem");
  44. MODULE_LICENSE("GPL");
  45. #define EXTENT_MERGE_SIZE 5
  46. static mode_t udf_convert_permissions(struct fileEntry *);
  47. static int udf_update_inode(struct inode *, int);
  48. static void udf_fill_inode(struct inode *, struct buffer_head *);
  49. static int udf_alloc_i_data(struct inode *inode, size_t size);
  50. static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
  51. sector_t *, int *);
  52. static int8_t udf_insert_aext(struct inode *, struct extent_position,
  53. struct kernel_lb_addr, uint32_t);
  54. static void udf_split_extents(struct inode *, int *, int, int,
  55. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  56. static void udf_prealloc_extents(struct inode *, int, int,
  57. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  58. static void udf_merge_extents(struct inode *,
  59. struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
  60. static void udf_update_extents(struct inode *,
  61. struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
  62. struct extent_position *);
  63. static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
  64. void udf_delete_inode(struct inode *inode)
  65. {
  66. truncate_inode_pages(&inode->i_data, 0);
  67. if (is_bad_inode(inode))
  68. goto no_delete;
  69. inode->i_size = 0;
  70. udf_truncate(inode);
  71. lock_kernel();
  72. udf_update_inode(inode, IS_SYNC(inode));
  73. udf_free_inode(inode);
  74. unlock_kernel();
  75. return;
  76. no_delete:
  77. clear_inode(inode);
  78. }
  79. /*
  80. * If we are going to release inode from memory, we discard preallocation and
  81. * truncate last inode extent to proper length. We could use drop_inode() but
  82. * it's called under inode_lock and thus we cannot mark inode dirty there. We
  83. * use clear_inode() but we have to make sure to write inode as it's not written
  84. * automatically.
  85. */
  86. void udf_clear_inode(struct inode *inode)
  87. {
  88. struct udf_inode_info *iinfo;
  89. if (!(inode->i_sb->s_flags & MS_RDONLY)) {
  90. lock_kernel();
  91. /* Discard preallocation for directories, symlinks, etc. */
  92. udf_discard_prealloc(inode);
  93. udf_truncate_tail_extent(inode);
  94. unlock_kernel();
  95. write_inode_now(inode, 0);
  96. invalidate_inode_buffers(inode);
  97. }
  98. iinfo = UDF_I(inode);
  99. kfree(iinfo->i_ext.i_data);
  100. iinfo->i_ext.i_data = NULL;
  101. }
  102. static int udf_writepage(struct page *page, struct writeback_control *wbc)
  103. {
  104. return block_write_full_page(page, udf_get_block, wbc);
  105. }
  106. static int udf_readpage(struct file *file, struct page *page)
  107. {
  108. return block_read_full_page(page, udf_get_block);
  109. }
  110. static int udf_write_begin(struct file *file, struct address_space *mapping,
  111. loff_t pos, unsigned len, unsigned flags,
  112. struct page **pagep, void **fsdata)
  113. {
  114. *pagep = NULL;
  115. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  116. udf_get_block);
  117. }
  118. static sector_t udf_bmap(struct address_space *mapping, sector_t block)
  119. {
  120. return generic_block_bmap(mapping, block, udf_get_block);
  121. }
  122. const struct address_space_operations udf_aops = {
  123. .readpage = udf_readpage,
  124. .writepage = udf_writepage,
  125. .sync_page = block_sync_page,
  126. .write_begin = udf_write_begin,
  127. .write_end = generic_write_end,
  128. .bmap = udf_bmap,
  129. };
  130. void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
  131. {
  132. struct page *page;
  133. char *kaddr;
  134. struct udf_inode_info *iinfo = UDF_I(inode);
  135. struct writeback_control udf_wbc = {
  136. .sync_mode = WB_SYNC_NONE,
  137. .nr_to_write = 1,
  138. };
  139. /* from now on we have normal address_space methods */
  140. inode->i_data.a_ops = &udf_aops;
  141. if (!iinfo->i_lenAlloc) {
  142. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  143. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  144. else
  145. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  146. mark_inode_dirty(inode);
  147. return;
  148. }
  149. page = grab_cache_page(inode->i_mapping, 0);
  150. BUG_ON(!PageLocked(page));
  151. if (!PageUptodate(page)) {
  152. kaddr = kmap(page);
  153. memset(kaddr + iinfo->i_lenAlloc, 0x00,
  154. PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
  155. memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
  156. iinfo->i_lenAlloc);
  157. flush_dcache_page(page);
  158. SetPageUptodate(page);
  159. kunmap(page);
  160. }
  161. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
  162. iinfo->i_lenAlloc);
  163. iinfo->i_lenAlloc = 0;
  164. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  165. iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
  166. else
  167. iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
  168. inode->i_data.a_ops->writepage(page, &udf_wbc);
  169. page_cache_release(page);
  170. mark_inode_dirty(inode);
  171. }
  172. struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
  173. int *err)
  174. {
  175. int newblock;
  176. struct buffer_head *dbh = NULL;
  177. struct kernel_lb_addr eloc;
  178. uint32_t elen;
  179. uint8_t alloctype;
  180. struct extent_position epos;
  181. struct udf_fileident_bh sfibh, dfibh;
  182. loff_t f_pos = udf_ext0_offset(inode);
  183. int size = udf_ext0_offset(inode) + inode->i_size;
  184. struct fileIdentDesc cfi, *sfi, *dfi;
  185. struct udf_inode_info *iinfo = UDF_I(inode);
  186. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
  187. alloctype = ICBTAG_FLAG_AD_SHORT;
  188. else
  189. alloctype = ICBTAG_FLAG_AD_LONG;
  190. if (!inode->i_size) {
  191. iinfo->i_alloc_type = alloctype;
  192. mark_inode_dirty(inode);
  193. return NULL;
  194. }
  195. /* alloc block, and copy data to it */
  196. *block = udf_new_block(inode->i_sb, inode,
  197. iinfo->i_location.partitionReferenceNum,
  198. iinfo->i_location.logicalBlockNum, err);
  199. if (!(*block))
  200. return NULL;
  201. newblock = udf_get_pblock(inode->i_sb, *block,
  202. iinfo->i_location.partitionReferenceNum,
  203. 0);
  204. if (!newblock)
  205. return NULL;
  206. dbh = udf_tgetblk(inode->i_sb, newblock);
  207. if (!dbh)
  208. return NULL;
  209. lock_buffer(dbh);
  210. memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
  211. set_buffer_uptodate(dbh);
  212. unlock_buffer(dbh);
  213. mark_buffer_dirty_inode(dbh, inode);
  214. sfibh.soffset = sfibh.eoffset =
  215. f_pos & (inode->i_sb->s_blocksize - 1);
  216. sfibh.sbh = sfibh.ebh = NULL;
  217. dfibh.soffset = dfibh.eoffset = 0;
  218. dfibh.sbh = dfibh.ebh = dbh;
  219. while (f_pos < size) {
  220. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  221. sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
  222. NULL, NULL, NULL);
  223. if (!sfi) {
  224. brelse(dbh);
  225. return NULL;
  226. }
  227. iinfo->i_alloc_type = alloctype;
  228. sfi->descTag.tagLocation = cpu_to_le32(*block);
  229. dfibh.soffset = dfibh.eoffset;
  230. dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
  231. dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
  232. if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
  233. sfi->fileIdent +
  234. le16_to_cpu(sfi->lengthOfImpUse))) {
  235. iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
  236. brelse(dbh);
  237. return NULL;
  238. }
  239. }
  240. mark_buffer_dirty_inode(dbh, inode);
  241. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
  242. iinfo->i_lenAlloc);
  243. iinfo->i_lenAlloc = 0;
  244. eloc.logicalBlockNum = *block;
  245. eloc.partitionReferenceNum =
  246. iinfo->i_location.partitionReferenceNum;
  247. elen = inode->i_sb->s_blocksize;
  248. iinfo->i_lenExtents = elen;
  249. epos.bh = NULL;
  250. epos.block = iinfo->i_location;
  251. epos.offset = udf_file_entry_alloc_offset(inode);
  252. udf_add_aext(inode, &epos, &eloc, elen, 0);
  253. /* UniqueID stuff */
  254. brelse(epos.bh);
  255. mark_inode_dirty(inode);
  256. return dbh;
  257. }
  258. static int udf_get_block(struct inode *inode, sector_t block,
  259. struct buffer_head *bh_result, int create)
  260. {
  261. int err, new;
  262. struct buffer_head *bh;
  263. sector_t phys = 0;
  264. struct udf_inode_info *iinfo;
  265. if (!create) {
  266. phys = udf_block_map(inode, block);
  267. if (phys)
  268. map_bh(bh_result, inode->i_sb, phys);
  269. return 0;
  270. }
  271. err = -EIO;
  272. new = 0;
  273. bh = NULL;
  274. lock_kernel();
  275. iinfo = UDF_I(inode);
  276. if (block == iinfo->i_next_alloc_block + 1) {
  277. iinfo->i_next_alloc_block++;
  278. iinfo->i_next_alloc_goal++;
  279. }
  280. err = 0;
  281. bh = inode_getblk(inode, block, &err, &phys, &new);
  282. BUG_ON(bh);
  283. if (err)
  284. goto abort;
  285. BUG_ON(!phys);
  286. if (new)
  287. set_buffer_new(bh_result);
  288. map_bh(bh_result, inode->i_sb, phys);
  289. abort:
  290. unlock_kernel();
  291. return err;
  292. }
  293. static struct buffer_head *udf_getblk(struct inode *inode, long block,
  294. int create, int *err)
  295. {
  296. struct buffer_head *bh;
  297. struct buffer_head dummy;
  298. dummy.b_state = 0;
  299. dummy.b_blocknr = -1000;
  300. *err = udf_get_block(inode, block, &dummy, create);
  301. if (!*err && buffer_mapped(&dummy)) {
  302. bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
  303. if (buffer_new(&dummy)) {
  304. lock_buffer(bh);
  305. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  306. set_buffer_uptodate(bh);
  307. unlock_buffer(bh);
  308. mark_buffer_dirty_inode(bh, inode);
  309. }
  310. return bh;
  311. }
  312. return NULL;
  313. }
  314. /* Extend the file by 'blocks' blocks, return the number of extents added */
  315. int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
  316. struct kernel_long_ad *last_ext, sector_t blocks)
  317. {
  318. sector_t add;
  319. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  320. struct super_block *sb = inode->i_sb;
  321. struct kernel_lb_addr prealloc_loc = {};
  322. int prealloc_len = 0;
  323. struct udf_inode_info *iinfo;
  324. /* The previous extent is fake and we should not extend by anything
  325. * - there's nothing to do... */
  326. if (!blocks && fake)
  327. return 0;
  328. iinfo = UDF_I(inode);
  329. /* Round the last extent up to a multiple of block size */
  330. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  331. last_ext->extLength =
  332. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  333. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  334. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  335. iinfo->i_lenExtents =
  336. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  337. ~(sb->s_blocksize - 1);
  338. }
  339. /* Last extent are just preallocated blocks? */
  340. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  341. EXT_NOT_RECORDED_ALLOCATED) {
  342. /* Save the extent so that we can reattach it to the end */
  343. prealloc_loc = last_ext->extLocation;
  344. prealloc_len = last_ext->extLength;
  345. /* Mark the extent as a hole */
  346. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  347. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  348. last_ext->extLocation.logicalBlockNum = 0;
  349. last_ext->extLocation.partitionReferenceNum = 0;
  350. }
  351. /* Can we merge with the previous extent? */
  352. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  353. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  354. add = ((1 << 30) - sb->s_blocksize -
  355. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  356. sb->s_blocksize_bits;
  357. if (add > blocks)
  358. add = blocks;
  359. blocks -= add;
  360. last_ext->extLength += add << sb->s_blocksize_bits;
  361. }
  362. if (fake) {
  363. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  364. last_ext->extLength, 1);
  365. count++;
  366. } else
  367. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  368. last_ext->extLength, 1);
  369. /* Managed to do everything necessary? */
  370. if (!blocks)
  371. goto out;
  372. /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
  373. last_ext->extLocation.logicalBlockNum = 0;
  374. last_ext->extLocation.partitionReferenceNum = 0;
  375. add = (1 << (30-sb->s_blocksize_bits)) - 1;
  376. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  377. (add << sb->s_blocksize_bits);
  378. /* Create enough extents to cover the whole hole */
  379. while (blocks > add) {
  380. blocks -= add;
  381. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  382. last_ext->extLength, 1) == -1)
  383. return -1;
  384. count++;
  385. }
  386. if (blocks) {
  387. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  388. (blocks << sb->s_blocksize_bits);
  389. if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
  390. last_ext->extLength, 1) == -1)
  391. return -1;
  392. count++;
  393. }
  394. out:
  395. /* Do we have some preallocated blocks saved? */
  396. if (prealloc_len) {
  397. if (udf_add_aext(inode, last_pos, &prealloc_loc,
  398. prealloc_len, 1) == -1)
  399. return -1;
  400. last_ext->extLocation = prealloc_loc;
  401. last_ext->extLength = prealloc_len;
  402. count++;
  403. }
  404. /* last_pos should point to the last written extent... */
  405. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  406. last_pos->offset -= sizeof(struct short_ad);
  407. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  408. last_pos->offset -= sizeof(struct long_ad);
  409. else
  410. return -1;
  411. return count;
  412. }
  413. static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
  414. int *err, sector_t *phys, int *new)
  415. {
  416. static sector_t last_block;
  417. struct buffer_head *result = NULL;
  418. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
  419. struct extent_position prev_epos, cur_epos, next_epos;
  420. int count = 0, startnum = 0, endnum = 0;
  421. uint32_t elen = 0, tmpelen;
  422. struct kernel_lb_addr eloc, tmpeloc;
  423. int c = 1;
  424. loff_t lbcount = 0, b_off = 0;
  425. uint32_t newblocknum, newblock;
  426. sector_t offset = 0;
  427. int8_t etype;
  428. struct udf_inode_info *iinfo = UDF_I(inode);
  429. int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
  430. int lastblock = 0;
  431. prev_epos.offset = udf_file_entry_alloc_offset(inode);
  432. prev_epos.block = iinfo->i_location;
  433. prev_epos.bh = NULL;
  434. cur_epos = next_epos = prev_epos;
  435. b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
  436. /* find the extent which contains the block we are looking for.
  437. alternate between laarr[0] and laarr[1] for locations of the
  438. current extent, and the previous extent */
  439. do {
  440. if (prev_epos.bh != cur_epos.bh) {
  441. brelse(prev_epos.bh);
  442. get_bh(cur_epos.bh);
  443. prev_epos.bh = cur_epos.bh;
  444. }
  445. if (cur_epos.bh != next_epos.bh) {
  446. brelse(cur_epos.bh);
  447. get_bh(next_epos.bh);
  448. cur_epos.bh = next_epos.bh;
  449. }
  450. lbcount += elen;
  451. prev_epos.block = cur_epos.block;
  452. cur_epos.block = next_epos.block;
  453. prev_epos.offset = cur_epos.offset;
  454. cur_epos.offset = next_epos.offset;
  455. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
  456. if (etype == -1)
  457. break;
  458. c = !c;
  459. laarr[c].extLength = (etype << 30) | elen;
  460. laarr[c].extLocation = eloc;
  461. if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  462. pgoal = eloc.logicalBlockNum +
  463. ((elen + inode->i_sb->s_blocksize - 1) >>
  464. inode->i_sb->s_blocksize_bits);
  465. count++;
  466. } while (lbcount + elen <= b_off);
  467. b_off -= lbcount;
  468. offset = b_off >> inode->i_sb->s_blocksize_bits;
  469. /*
  470. * Move prev_epos and cur_epos into indirect extent if we are at
  471. * the pointer to it
  472. */
  473. udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
  474. udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
  475. /* if the extent is allocated and recorded, return the block
  476. if the extent is not a multiple of the blocksize, round up */
  477. if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
  478. if (elen & (inode->i_sb->s_blocksize - 1)) {
  479. elen = EXT_RECORDED_ALLOCATED |
  480. ((elen + inode->i_sb->s_blocksize - 1) &
  481. ~(inode->i_sb->s_blocksize - 1));
  482. etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
  483. }
  484. brelse(prev_epos.bh);
  485. brelse(cur_epos.bh);
  486. brelse(next_epos.bh);
  487. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  488. *phys = newblock;
  489. return NULL;
  490. }
  491. last_block = block;
  492. /* Are we beyond EOF? */
  493. if (etype == -1) {
  494. int ret;
  495. if (count) {
  496. if (c)
  497. laarr[0] = laarr[1];
  498. startnum = 1;
  499. } else {
  500. /* Create a fake extent when there's not one */
  501. memset(&laarr[0].extLocation, 0x00,
  502. sizeof(struct kernel_lb_addr));
  503. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  504. /* Will udf_extend_file() create real extent from
  505. a fake one? */
  506. startnum = (offset > 0);
  507. }
  508. /* Create extents for the hole between EOF and offset */
  509. ret = udf_extend_file(inode, &prev_epos, laarr, offset);
  510. if (ret == -1) {
  511. brelse(prev_epos.bh);
  512. brelse(cur_epos.bh);
  513. brelse(next_epos.bh);
  514. /* We don't really know the error here so we just make
  515. * something up */
  516. *err = -ENOSPC;
  517. return NULL;
  518. }
  519. c = 0;
  520. offset = 0;
  521. count += ret;
  522. /* We are not covered by a preallocated extent? */
  523. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  524. EXT_NOT_RECORDED_ALLOCATED) {
  525. /* Is there any real extent? - otherwise we overwrite
  526. * the fake one... */
  527. if (count)
  528. c = !c;
  529. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  530. inode->i_sb->s_blocksize;
  531. memset(&laarr[c].extLocation, 0x00,
  532. sizeof(struct kernel_lb_addr));
  533. count++;
  534. endnum++;
  535. }
  536. endnum = c + 1;
  537. lastblock = 1;
  538. } else {
  539. endnum = startnum = ((count > 2) ? 2 : count);
  540. /* if the current extent is in position 0,
  541. swap it with the previous */
  542. if (!c && count != 1) {
  543. laarr[2] = laarr[0];
  544. laarr[0] = laarr[1];
  545. laarr[1] = laarr[2];
  546. c = 1;
  547. }
  548. /* if the current block is located in an extent,
  549. read the next extent */
  550. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  551. if (etype != -1) {
  552. laarr[c + 1].extLength = (etype << 30) | elen;
  553. laarr[c + 1].extLocation = eloc;
  554. count++;
  555. startnum++;
  556. endnum++;
  557. } else
  558. lastblock = 1;
  559. }
  560. /* if the current extent is not recorded but allocated, get the
  561. * block in the extent corresponding to the requested block */
  562. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  563. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  564. else { /* otherwise, allocate a new block */
  565. if (iinfo->i_next_alloc_block == block)
  566. goal = iinfo->i_next_alloc_goal;
  567. if (!goal) {
  568. if (!(goal = pgoal)) /* XXX: what was intended here? */
  569. goal = iinfo->i_location.logicalBlockNum + 1;
  570. }
  571. newblocknum = udf_new_block(inode->i_sb, inode,
  572. iinfo->i_location.partitionReferenceNum,
  573. goal, err);
  574. if (!newblocknum) {
  575. brelse(prev_epos.bh);
  576. *err = -ENOSPC;
  577. return NULL;
  578. }
  579. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  580. }
  581. /* if the extent the requsted block is located in contains multiple
  582. * blocks, split the extent into at most three extents. blocks prior
  583. * to requested block, requested block, and blocks after requested
  584. * block */
  585. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  586. #ifdef UDF_PREALLOCATE
  587. /* preallocate blocks */
  588. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  589. #endif
  590. /* merge any continuous blocks in laarr */
  591. udf_merge_extents(inode, laarr, &endnum);
  592. /* write back the new extents, inserting new extents if the new number
  593. * of extents is greater than the old number, and deleting extents if
  594. * the new number of extents is less than the old number */
  595. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  596. brelse(prev_epos.bh);
  597. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  598. iinfo->i_location.partitionReferenceNum, 0);
  599. if (!newblock)
  600. return NULL;
  601. *phys = newblock;
  602. *err = 0;
  603. *new = 1;
  604. iinfo->i_next_alloc_block = block;
  605. iinfo->i_next_alloc_goal = newblocknum;
  606. inode->i_ctime = current_fs_time(inode->i_sb);
  607. if (IS_SYNC(inode))
  608. udf_sync_inode(inode);
  609. else
  610. mark_inode_dirty(inode);
  611. return result;
  612. }
  613. static void udf_split_extents(struct inode *inode, int *c, int offset,
  614. int newblocknum,
  615. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  616. int *endnum)
  617. {
  618. unsigned long blocksize = inode->i_sb->s_blocksize;
  619. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  620. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  621. (laarr[*c].extLength >> 30) ==
  622. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  623. int curr = *c;
  624. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  625. blocksize - 1) >> blocksize_bits;
  626. int8_t etype = (laarr[curr].extLength >> 30);
  627. if (blen == 1)
  628. ;
  629. else if (!offset || blen == offset + 1) {
  630. laarr[curr + 2] = laarr[curr + 1];
  631. laarr[curr + 1] = laarr[curr];
  632. } else {
  633. laarr[curr + 3] = laarr[curr + 1];
  634. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  635. }
  636. if (offset) {
  637. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  638. udf_free_blocks(inode->i_sb, inode,
  639. &laarr[curr].extLocation,
  640. 0, offset);
  641. laarr[curr].extLength =
  642. EXT_NOT_RECORDED_NOT_ALLOCATED |
  643. (offset << blocksize_bits);
  644. laarr[curr].extLocation.logicalBlockNum = 0;
  645. laarr[curr].extLocation.
  646. partitionReferenceNum = 0;
  647. } else
  648. laarr[curr].extLength = (etype << 30) |
  649. (offset << blocksize_bits);
  650. curr++;
  651. (*c)++;
  652. (*endnum)++;
  653. }
  654. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  655. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  656. laarr[curr].extLocation.partitionReferenceNum =
  657. UDF_I(inode)->i_location.partitionReferenceNum;
  658. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  659. blocksize;
  660. curr++;
  661. if (blen != offset + 1) {
  662. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  663. laarr[curr].extLocation.logicalBlockNum +=
  664. offset + 1;
  665. laarr[curr].extLength = (etype << 30) |
  666. ((blen - (offset + 1)) << blocksize_bits);
  667. curr++;
  668. (*endnum)++;
  669. }
  670. }
  671. }
  672. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  673. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  674. int *endnum)
  675. {
  676. int start, length = 0, currlength = 0, i;
  677. if (*endnum >= (c + 1)) {
  678. if (!lastblock)
  679. return;
  680. else
  681. start = c;
  682. } else {
  683. if ((laarr[c + 1].extLength >> 30) ==
  684. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  685. start = c + 1;
  686. length = currlength =
  687. (((laarr[c + 1].extLength &
  688. UDF_EXTENT_LENGTH_MASK) +
  689. inode->i_sb->s_blocksize - 1) >>
  690. inode->i_sb->s_blocksize_bits);
  691. } else
  692. start = c;
  693. }
  694. for (i = start + 1; i <= *endnum; i++) {
  695. if (i == *endnum) {
  696. if (lastblock)
  697. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  698. } else if ((laarr[i].extLength >> 30) ==
  699. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  700. length += (((laarr[i].extLength &
  701. UDF_EXTENT_LENGTH_MASK) +
  702. inode->i_sb->s_blocksize - 1) >>
  703. inode->i_sb->s_blocksize_bits);
  704. } else
  705. break;
  706. }
  707. if (length) {
  708. int next = laarr[start].extLocation.logicalBlockNum +
  709. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  710. inode->i_sb->s_blocksize - 1) >>
  711. inode->i_sb->s_blocksize_bits);
  712. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  713. laarr[start].extLocation.partitionReferenceNum,
  714. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  715. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  716. currlength);
  717. if (numalloc) {
  718. if (start == (c + 1))
  719. laarr[start].extLength +=
  720. (numalloc <<
  721. inode->i_sb->s_blocksize_bits);
  722. else {
  723. memmove(&laarr[c + 2], &laarr[c + 1],
  724. sizeof(struct long_ad) * (*endnum - (c + 1)));
  725. (*endnum)++;
  726. laarr[c + 1].extLocation.logicalBlockNum = next;
  727. laarr[c + 1].extLocation.partitionReferenceNum =
  728. laarr[c].extLocation.
  729. partitionReferenceNum;
  730. laarr[c + 1].extLength =
  731. EXT_NOT_RECORDED_ALLOCATED |
  732. (numalloc <<
  733. inode->i_sb->s_blocksize_bits);
  734. start = c + 1;
  735. }
  736. for (i = start + 1; numalloc && i < *endnum; i++) {
  737. int elen = ((laarr[i].extLength &
  738. UDF_EXTENT_LENGTH_MASK) +
  739. inode->i_sb->s_blocksize - 1) >>
  740. inode->i_sb->s_blocksize_bits;
  741. if (elen > numalloc) {
  742. laarr[i].extLength -=
  743. (numalloc <<
  744. inode->i_sb->s_blocksize_bits);
  745. numalloc = 0;
  746. } else {
  747. numalloc -= elen;
  748. if (*endnum > (i + 1))
  749. memmove(&laarr[i],
  750. &laarr[i + 1],
  751. sizeof(struct long_ad) *
  752. (*endnum - (i + 1)));
  753. i--;
  754. (*endnum)--;
  755. }
  756. }
  757. UDF_I(inode)->i_lenExtents +=
  758. numalloc << inode->i_sb->s_blocksize_bits;
  759. }
  760. }
  761. }
  762. static void udf_merge_extents(struct inode *inode,
  763. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  764. int *endnum)
  765. {
  766. int i;
  767. unsigned long blocksize = inode->i_sb->s_blocksize;
  768. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  769. for (i = 0; i < (*endnum - 1); i++) {
  770. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  771. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  772. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  773. (((li->extLength >> 30) ==
  774. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  775. ((lip1->extLocation.logicalBlockNum -
  776. li->extLocation.logicalBlockNum) ==
  777. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  778. blocksize - 1) >> blocksize_bits)))) {
  779. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  780. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  781. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  782. lip1->extLength = (lip1->extLength -
  783. (li->extLength &
  784. UDF_EXTENT_LENGTH_MASK) +
  785. UDF_EXTENT_LENGTH_MASK) &
  786. ~(blocksize - 1);
  787. li->extLength = (li->extLength &
  788. UDF_EXTENT_FLAG_MASK) +
  789. (UDF_EXTENT_LENGTH_MASK + 1) -
  790. blocksize;
  791. lip1->extLocation.logicalBlockNum =
  792. li->extLocation.logicalBlockNum +
  793. ((li->extLength &
  794. UDF_EXTENT_LENGTH_MASK) >>
  795. blocksize_bits);
  796. } else {
  797. li->extLength = lip1->extLength +
  798. (((li->extLength &
  799. UDF_EXTENT_LENGTH_MASK) +
  800. blocksize - 1) & ~(blocksize - 1));
  801. if (*endnum > (i + 2))
  802. memmove(&laarr[i + 1], &laarr[i + 2],
  803. sizeof(struct long_ad) *
  804. (*endnum - (i + 2)));
  805. i--;
  806. (*endnum)--;
  807. }
  808. } else if (((li->extLength >> 30) ==
  809. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  810. ((lip1->extLength >> 30) ==
  811. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  812. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  813. ((li->extLength &
  814. UDF_EXTENT_LENGTH_MASK) +
  815. blocksize - 1) >> blocksize_bits);
  816. li->extLocation.logicalBlockNum = 0;
  817. li->extLocation.partitionReferenceNum = 0;
  818. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  819. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  820. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  821. lip1->extLength = (lip1->extLength -
  822. (li->extLength &
  823. UDF_EXTENT_LENGTH_MASK) +
  824. UDF_EXTENT_LENGTH_MASK) &
  825. ~(blocksize - 1);
  826. li->extLength = (li->extLength &
  827. UDF_EXTENT_FLAG_MASK) +
  828. (UDF_EXTENT_LENGTH_MASK + 1) -
  829. blocksize;
  830. } else {
  831. li->extLength = lip1->extLength +
  832. (((li->extLength &
  833. UDF_EXTENT_LENGTH_MASK) +
  834. blocksize - 1) & ~(blocksize - 1));
  835. if (*endnum > (i + 2))
  836. memmove(&laarr[i + 1], &laarr[i + 2],
  837. sizeof(struct long_ad) *
  838. (*endnum - (i + 2)));
  839. i--;
  840. (*endnum)--;
  841. }
  842. } else if ((li->extLength >> 30) ==
  843. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  844. udf_free_blocks(inode->i_sb, inode,
  845. &li->extLocation, 0,
  846. ((li->extLength &
  847. UDF_EXTENT_LENGTH_MASK) +
  848. blocksize - 1) >> blocksize_bits);
  849. li->extLocation.logicalBlockNum = 0;
  850. li->extLocation.partitionReferenceNum = 0;
  851. li->extLength = (li->extLength &
  852. UDF_EXTENT_LENGTH_MASK) |
  853. EXT_NOT_RECORDED_NOT_ALLOCATED;
  854. }
  855. }
  856. }
  857. static void udf_update_extents(struct inode *inode,
  858. struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
  859. int startnum, int endnum,
  860. struct extent_position *epos)
  861. {
  862. int start = 0, i;
  863. struct kernel_lb_addr tmploc;
  864. uint32_t tmplen;
  865. if (startnum > endnum) {
  866. for (i = 0; i < (startnum - endnum); i++)
  867. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  868. laarr[i].extLength);
  869. } else if (startnum < endnum) {
  870. for (i = 0; i < (endnum - startnum); i++) {
  871. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  872. laarr[i].extLength);
  873. udf_next_aext(inode, epos, &laarr[i].extLocation,
  874. &laarr[i].extLength, 1);
  875. start++;
  876. }
  877. }
  878. for (i = start; i < endnum; i++) {
  879. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  880. udf_write_aext(inode, epos, &laarr[i].extLocation,
  881. laarr[i].extLength, 1);
  882. }
  883. }
  884. struct buffer_head *udf_bread(struct inode *inode, int block,
  885. int create, int *err)
  886. {
  887. struct buffer_head *bh = NULL;
  888. bh = udf_getblk(inode, block, create, err);
  889. if (!bh)
  890. return NULL;
  891. if (buffer_uptodate(bh))
  892. return bh;
  893. ll_rw_block(READ, 1, &bh);
  894. wait_on_buffer(bh);
  895. if (buffer_uptodate(bh))
  896. return bh;
  897. brelse(bh);
  898. *err = -EIO;
  899. return NULL;
  900. }
  901. void udf_truncate(struct inode *inode)
  902. {
  903. int offset;
  904. int err;
  905. struct udf_inode_info *iinfo;
  906. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  907. S_ISLNK(inode->i_mode)))
  908. return;
  909. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  910. return;
  911. lock_kernel();
  912. iinfo = UDF_I(inode);
  913. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  914. if (inode->i_sb->s_blocksize <
  915. (udf_file_entry_alloc_offset(inode) +
  916. inode->i_size)) {
  917. udf_expand_file_adinicb(inode, inode->i_size, &err);
  918. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  919. inode->i_size = iinfo->i_lenAlloc;
  920. unlock_kernel();
  921. return;
  922. } else
  923. udf_truncate_extents(inode);
  924. } else {
  925. offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
  926. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
  927. 0x00, inode->i_sb->s_blocksize -
  928. offset - udf_file_entry_alloc_offset(inode));
  929. iinfo->i_lenAlloc = inode->i_size;
  930. }
  931. } else {
  932. block_truncate_page(inode->i_mapping, inode->i_size,
  933. udf_get_block);
  934. udf_truncate_extents(inode);
  935. }
  936. inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
  937. if (IS_SYNC(inode))
  938. udf_sync_inode(inode);
  939. else
  940. mark_inode_dirty(inode);
  941. unlock_kernel();
  942. }
  943. static void __udf_read_inode(struct inode *inode)
  944. {
  945. struct buffer_head *bh = NULL;
  946. struct fileEntry *fe;
  947. uint16_t ident;
  948. struct udf_inode_info *iinfo = UDF_I(inode);
  949. /*
  950. * Set defaults, but the inode is still incomplete!
  951. * Note: get_new_inode() sets the following on a new inode:
  952. * i_sb = sb
  953. * i_no = ino
  954. * i_flags = sb->s_flags
  955. * i_state = 0
  956. * clean_inode(): zero fills and sets
  957. * i_count = 1
  958. * i_nlink = 1
  959. * i_op = NULL;
  960. */
  961. bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
  962. if (!bh) {
  963. printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
  964. inode->i_ino);
  965. make_bad_inode(inode);
  966. return;
  967. }
  968. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  969. ident != TAG_IDENT_USE) {
  970. printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
  971. "failed ident=%d\n", inode->i_ino, ident);
  972. brelse(bh);
  973. make_bad_inode(inode);
  974. return;
  975. }
  976. fe = (struct fileEntry *)bh->b_data;
  977. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  978. struct buffer_head *ibh;
  979. ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
  980. &ident);
  981. if (ident == TAG_IDENT_IE && ibh) {
  982. struct buffer_head *nbh = NULL;
  983. struct kernel_lb_addr loc;
  984. struct indirectEntry *ie;
  985. ie = (struct indirectEntry *)ibh->b_data;
  986. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  987. if (ie->indirectICB.extLength &&
  988. (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
  989. &ident))) {
  990. if (ident == TAG_IDENT_FE ||
  991. ident == TAG_IDENT_EFE) {
  992. memcpy(&iinfo->i_location,
  993. &loc,
  994. sizeof(struct kernel_lb_addr));
  995. brelse(bh);
  996. brelse(ibh);
  997. brelse(nbh);
  998. __udf_read_inode(inode);
  999. return;
  1000. }
  1001. brelse(nbh);
  1002. }
  1003. }
  1004. brelse(ibh);
  1005. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1006. printk(KERN_ERR "udf: unsupported strategy type: %d\n",
  1007. le16_to_cpu(fe->icbTag.strategyType));
  1008. brelse(bh);
  1009. make_bad_inode(inode);
  1010. return;
  1011. }
  1012. udf_fill_inode(inode, bh);
  1013. brelse(bh);
  1014. }
  1015. static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
  1016. {
  1017. struct fileEntry *fe;
  1018. struct extendedFileEntry *efe;
  1019. int offset;
  1020. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1021. struct udf_inode_info *iinfo = UDF_I(inode);
  1022. fe = (struct fileEntry *)bh->b_data;
  1023. efe = (struct extendedFileEntry *)bh->b_data;
  1024. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1025. iinfo->i_strat4096 = 0;
  1026. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1027. iinfo->i_strat4096 = 1;
  1028. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1029. ICBTAG_FLAG_AD_MASK;
  1030. iinfo->i_unique = 0;
  1031. iinfo->i_lenEAttr = 0;
  1032. iinfo->i_lenExtents = 0;
  1033. iinfo->i_lenAlloc = 0;
  1034. iinfo->i_next_alloc_block = 0;
  1035. iinfo->i_next_alloc_goal = 0;
  1036. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1037. iinfo->i_efe = 1;
  1038. iinfo->i_use = 0;
  1039. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1040. sizeof(struct extendedFileEntry))) {
  1041. make_bad_inode(inode);
  1042. return;
  1043. }
  1044. memcpy(iinfo->i_ext.i_data,
  1045. bh->b_data + sizeof(struct extendedFileEntry),
  1046. inode->i_sb->s_blocksize -
  1047. sizeof(struct extendedFileEntry));
  1048. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1049. iinfo->i_efe = 0;
  1050. iinfo->i_use = 0;
  1051. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1052. sizeof(struct fileEntry))) {
  1053. make_bad_inode(inode);
  1054. return;
  1055. }
  1056. memcpy(iinfo->i_ext.i_data,
  1057. bh->b_data + sizeof(struct fileEntry),
  1058. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1059. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1060. iinfo->i_efe = 0;
  1061. iinfo->i_use = 1;
  1062. iinfo->i_lenAlloc = le32_to_cpu(
  1063. ((struct unallocSpaceEntry *)bh->b_data)->
  1064. lengthAllocDescs);
  1065. if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
  1066. sizeof(struct unallocSpaceEntry))) {
  1067. make_bad_inode(inode);
  1068. return;
  1069. }
  1070. memcpy(iinfo->i_ext.i_data,
  1071. bh->b_data + sizeof(struct unallocSpaceEntry),
  1072. inode->i_sb->s_blocksize -
  1073. sizeof(struct unallocSpaceEntry));
  1074. return;
  1075. }
  1076. inode->i_uid = le32_to_cpu(fe->uid);
  1077. if (inode->i_uid == -1 ||
  1078. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
  1079. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1080. inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
  1081. inode->i_gid = le32_to_cpu(fe->gid);
  1082. if (inode->i_gid == -1 ||
  1083. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
  1084. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1085. inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
  1086. inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
  1087. if (!inode->i_nlink)
  1088. inode->i_nlink = 1;
  1089. inode->i_size = le64_to_cpu(fe->informationLength);
  1090. iinfo->i_lenExtents = inode->i_size;
  1091. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1092. sbi->s_fmode != UDF_INVALID_MODE)
  1093. inode->i_mode = sbi->s_fmode;
  1094. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1095. sbi->s_dmode != UDF_INVALID_MODE)
  1096. inode->i_mode = sbi->s_dmode;
  1097. else
  1098. inode->i_mode = udf_convert_permissions(fe);
  1099. inode->i_mode &= ~sbi->s_umask;
  1100. if (iinfo->i_efe == 0) {
  1101. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1102. (inode->i_sb->s_blocksize_bits - 9);
  1103. if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
  1104. inode->i_atime = sbi->s_record_time;
  1105. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1106. fe->modificationTime))
  1107. inode->i_mtime = sbi->s_record_time;
  1108. if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
  1109. inode->i_ctime = sbi->s_record_time;
  1110. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1111. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1112. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1113. offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
  1114. } else {
  1115. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1116. (inode->i_sb->s_blocksize_bits - 9);
  1117. if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
  1118. inode->i_atime = sbi->s_record_time;
  1119. if (!udf_disk_stamp_to_time(&inode->i_mtime,
  1120. efe->modificationTime))
  1121. inode->i_mtime = sbi->s_record_time;
  1122. if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
  1123. iinfo->i_crtime = sbi->s_record_time;
  1124. if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
  1125. inode->i_ctime = sbi->s_record_time;
  1126. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1127. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1128. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1129. offset = sizeof(struct extendedFileEntry) +
  1130. iinfo->i_lenEAttr;
  1131. }
  1132. switch (fe->icbTag.fileType) {
  1133. case ICBTAG_FILE_TYPE_DIRECTORY:
  1134. inode->i_op = &udf_dir_inode_operations;
  1135. inode->i_fop = &udf_dir_operations;
  1136. inode->i_mode |= S_IFDIR;
  1137. inc_nlink(inode);
  1138. break;
  1139. case ICBTAG_FILE_TYPE_REALTIME:
  1140. case ICBTAG_FILE_TYPE_REGULAR:
  1141. case ICBTAG_FILE_TYPE_UNDEF:
  1142. case ICBTAG_FILE_TYPE_VAT20:
  1143. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1144. inode->i_data.a_ops = &udf_adinicb_aops;
  1145. else
  1146. inode->i_data.a_ops = &udf_aops;
  1147. inode->i_op = &udf_file_inode_operations;
  1148. inode->i_fop = &udf_file_operations;
  1149. inode->i_mode |= S_IFREG;
  1150. break;
  1151. case ICBTAG_FILE_TYPE_BLOCK:
  1152. inode->i_mode |= S_IFBLK;
  1153. break;
  1154. case ICBTAG_FILE_TYPE_CHAR:
  1155. inode->i_mode |= S_IFCHR;
  1156. break;
  1157. case ICBTAG_FILE_TYPE_FIFO:
  1158. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1159. break;
  1160. case ICBTAG_FILE_TYPE_SOCKET:
  1161. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1162. break;
  1163. case ICBTAG_FILE_TYPE_SYMLINK:
  1164. inode->i_data.a_ops = &udf_symlink_aops;
  1165. inode->i_op = &page_symlink_inode_operations;
  1166. inode->i_mode = S_IFLNK | S_IRWXUGO;
  1167. break;
  1168. case ICBTAG_FILE_TYPE_MAIN:
  1169. udf_debug("METADATA FILE-----\n");
  1170. break;
  1171. case ICBTAG_FILE_TYPE_MIRROR:
  1172. udf_debug("METADATA MIRROR FILE-----\n");
  1173. break;
  1174. case ICBTAG_FILE_TYPE_BITMAP:
  1175. udf_debug("METADATA BITMAP FILE-----\n");
  1176. break;
  1177. default:
  1178. printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
  1179. "file type=%d\n", inode->i_ino,
  1180. fe->icbTag.fileType);
  1181. make_bad_inode(inode);
  1182. return;
  1183. }
  1184. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1185. struct deviceSpec *dsea =
  1186. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1187. if (dsea) {
  1188. init_special_inode(inode, inode->i_mode,
  1189. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1190. le32_to_cpu(dsea->minorDeviceIdent)));
  1191. /* Developer ID ??? */
  1192. } else
  1193. make_bad_inode(inode);
  1194. }
  1195. }
  1196. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1197. {
  1198. struct udf_inode_info *iinfo = UDF_I(inode);
  1199. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1200. if (!iinfo->i_ext.i_data) {
  1201. printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
  1202. "no free memory\n", inode->i_ino);
  1203. return -ENOMEM;
  1204. }
  1205. return 0;
  1206. }
  1207. static mode_t udf_convert_permissions(struct fileEntry *fe)
  1208. {
  1209. mode_t mode;
  1210. uint32_t permissions;
  1211. uint32_t flags;
  1212. permissions = le32_to_cpu(fe->permissions);
  1213. flags = le16_to_cpu(fe->icbTag.flags);
  1214. mode = ((permissions) & S_IRWXO) |
  1215. ((permissions >> 2) & S_IRWXG) |
  1216. ((permissions >> 4) & S_IRWXU) |
  1217. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1218. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1219. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1220. return mode;
  1221. }
  1222. int udf_write_inode(struct inode *inode, int sync)
  1223. {
  1224. int ret;
  1225. lock_kernel();
  1226. ret = udf_update_inode(inode, sync);
  1227. unlock_kernel();
  1228. return ret;
  1229. }
  1230. int udf_sync_inode(struct inode *inode)
  1231. {
  1232. return udf_update_inode(inode, 1);
  1233. }
  1234. static int udf_update_inode(struct inode *inode, int do_sync)
  1235. {
  1236. struct buffer_head *bh = NULL;
  1237. struct fileEntry *fe;
  1238. struct extendedFileEntry *efe;
  1239. uint32_t udfperms;
  1240. uint16_t icbflags;
  1241. uint16_t crclen;
  1242. int err = 0;
  1243. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1244. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1245. struct udf_inode_info *iinfo = UDF_I(inode);
  1246. bh = udf_tread(inode->i_sb,
  1247. udf_get_lb_pblock(inode->i_sb,
  1248. &iinfo->i_location, 0));
  1249. if (!bh) {
  1250. udf_debug("bread failure\n");
  1251. return -EIO;
  1252. }
  1253. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  1254. fe = (struct fileEntry *)bh->b_data;
  1255. efe = (struct extendedFileEntry *)bh->b_data;
  1256. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1257. struct unallocSpaceEntry *use =
  1258. (struct unallocSpaceEntry *)bh->b_data;
  1259. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1260. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1261. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1262. sizeof(struct unallocSpaceEntry));
  1263. crclen = sizeof(struct unallocSpaceEntry) +
  1264. iinfo->i_lenAlloc - sizeof(struct tag);
  1265. use->descTag.tagLocation = cpu_to_le32(
  1266. iinfo->i_location.
  1267. logicalBlockNum);
  1268. use->descTag.descCRCLength = cpu_to_le16(crclen);
  1269. use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
  1270. sizeof(struct tag),
  1271. crclen));
  1272. use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
  1273. mark_buffer_dirty(bh);
  1274. brelse(bh);
  1275. return err;
  1276. }
  1277. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1278. fe->uid = cpu_to_le32(-1);
  1279. else
  1280. fe->uid = cpu_to_le32(inode->i_uid);
  1281. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1282. fe->gid = cpu_to_le32(-1);
  1283. else
  1284. fe->gid = cpu_to_le32(inode->i_gid);
  1285. udfperms = ((inode->i_mode & S_IRWXO)) |
  1286. ((inode->i_mode & S_IRWXG) << 2) |
  1287. ((inode->i_mode & S_IRWXU) << 4);
  1288. udfperms |= (le32_to_cpu(fe->permissions) &
  1289. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1290. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1291. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1292. fe->permissions = cpu_to_le32(udfperms);
  1293. if (S_ISDIR(inode->i_mode))
  1294. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1295. else
  1296. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1297. fe->informationLength = cpu_to_le64(inode->i_size);
  1298. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1299. struct regid *eid;
  1300. struct deviceSpec *dsea =
  1301. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1302. if (!dsea) {
  1303. dsea = (struct deviceSpec *)
  1304. udf_add_extendedattr(inode,
  1305. sizeof(struct deviceSpec) +
  1306. sizeof(struct regid), 12, 0x3);
  1307. dsea->attrType = cpu_to_le32(12);
  1308. dsea->attrSubtype = 1;
  1309. dsea->attrLength = cpu_to_le32(
  1310. sizeof(struct deviceSpec) +
  1311. sizeof(struct regid));
  1312. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1313. }
  1314. eid = (struct regid *)dsea->impUse;
  1315. memset(eid, 0, sizeof(struct regid));
  1316. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1317. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1318. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1319. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1320. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1321. }
  1322. if (iinfo->i_efe == 0) {
  1323. memcpy(bh->b_data + sizeof(struct fileEntry),
  1324. iinfo->i_ext.i_data,
  1325. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1326. fe->logicalBlocksRecorded = cpu_to_le64(
  1327. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1328. (blocksize_bits - 9));
  1329. udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
  1330. udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
  1331. udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
  1332. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1333. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1334. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1335. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1336. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1337. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1338. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1339. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1340. crclen = sizeof(struct fileEntry);
  1341. } else {
  1342. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1343. iinfo->i_ext.i_data,
  1344. inode->i_sb->s_blocksize -
  1345. sizeof(struct extendedFileEntry));
  1346. efe->objectSize = cpu_to_le64(inode->i_size);
  1347. efe->logicalBlocksRecorded = cpu_to_le64(
  1348. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1349. (blocksize_bits - 9));
  1350. if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
  1351. (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
  1352. iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
  1353. iinfo->i_crtime = inode->i_atime;
  1354. if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
  1355. (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
  1356. iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
  1357. iinfo->i_crtime = inode->i_mtime;
  1358. if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
  1359. (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
  1360. iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
  1361. iinfo->i_crtime = inode->i_ctime;
  1362. udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
  1363. udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
  1364. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1365. udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
  1366. memset(&(efe->impIdent), 0, sizeof(struct regid));
  1367. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1368. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1369. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1370. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1371. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1372. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1373. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1374. crclen = sizeof(struct extendedFileEntry);
  1375. }
  1376. if (iinfo->i_strat4096) {
  1377. fe->icbTag.strategyType = cpu_to_le16(4096);
  1378. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1379. fe->icbTag.numEntries = cpu_to_le16(2);
  1380. } else {
  1381. fe->icbTag.strategyType = cpu_to_le16(4);
  1382. fe->icbTag.numEntries = cpu_to_le16(1);
  1383. }
  1384. if (S_ISDIR(inode->i_mode))
  1385. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1386. else if (S_ISREG(inode->i_mode))
  1387. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1388. else if (S_ISLNK(inode->i_mode))
  1389. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1390. else if (S_ISBLK(inode->i_mode))
  1391. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1392. else if (S_ISCHR(inode->i_mode))
  1393. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1394. else if (S_ISFIFO(inode->i_mode))
  1395. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1396. else if (S_ISSOCK(inode->i_mode))
  1397. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1398. icbflags = iinfo->i_alloc_type |
  1399. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1400. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1401. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1402. (le16_to_cpu(fe->icbTag.flags) &
  1403. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1404. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1405. fe->icbTag.flags = cpu_to_le16(icbflags);
  1406. if (sbi->s_udfrev >= 0x0200)
  1407. fe->descTag.descVersion = cpu_to_le16(3);
  1408. else
  1409. fe->descTag.descVersion = cpu_to_le16(2);
  1410. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1411. fe->descTag.tagLocation = cpu_to_le32(
  1412. iinfo->i_location.logicalBlockNum);
  1413. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
  1414. sizeof(struct tag);
  1415. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1416. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1417. crclen));
  1418. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1419. /* write the data blocks */
  1420. mark_buffer_dirty(bh);
  1421. if (do_sync) {
  1422. sync_dirty_buffer(bh);
  1423. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1424. printk(KERN_WARNING "IO error syncing udf inode "
  1425. "[%s:%08lx]\n", inode->i_sb->s_id,
  1426. inode->i_ino);
  1427. err = -EIO;
  1428. }
  1429. }
  1430. brelse(bh);
  1431. return err;
  1432. }
  1433. struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
  1434. {
  1435. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1436. struct inode *inode = iget_locked(sb, block);
  1437. if (!inode)
  1438. return NULL;
  1439. if (inode->i_state & I_NEW) {
  1440. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1441. __udf_read_inode(inode);
  1442. unlock_new_inode(inode);
  1443. }
  1444. if (is_bad_inode(inode))
  1445. goto out_iput;
  1446. if (ino->logicalBlockNum >= UDF_SB(sb)->
  1447. s_partmaps[ino->partitionReferenceNum].s_partition_len) {
  1448. udf_debug("block=%d, partition=%d out of range\n",
  1449. ino->logicalBlockNum, ino->partitionReferenceNum);
  1450. make_bad_inode(inode);
  1451. goto out_iput;
  1452. }
  1453. return inode;
  1454. out_iput:
  1455. iput(inode);
  1456. return NULL;
  1457. }
  1458. int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
  1459. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1460. {
  1461. int adsize;
  1462. struct short_ad *sad = NULL;
  1463. struct long_ad *lad = NULL;
  1464. struct allocExtDesc *aed;
  1465. int8_t etype;
  1466. uint8_t *ptr;
  1467. struct udf_inode_info *iinfo = UDF_I(inode);
  1468. if (!epos->bh)
  1469. ptr = iinfo->i_ext.i_data + epos->offset -
  1470. udf_file_entry_alloc_offset(inode) +
  1471. iinfo->i_lenEAttr;
  1472. else
  1473. ptr = epos->bh->b_data + epos->offset;
  1474. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1475. adsize = sizeof(struct short_ad);
  1476. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1477. adsize = sizeof(struct long_ad);
  1478. else
  1479. return -1;
  1480. if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
  1481. char *sptr, *dptr;
  1482. struct buffer_head *nbh;
  1483. int err, loffset;
  1484. struct kernel_lb_addr obloc = epos->block;
  1485. epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
  1486. obloc.partitionReferenceNum,
  1487. obloc.logicalBlockNum, &err);
  1488. if (!epos->block.logicalBlockNum)
  1489. return -1;
  1490. nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
  1491. &epos->block,
  1492. 0));
  1493. if (!nbh)
  1494. return -1;
  1495. lock_buffer(nbh);
  1496. memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
  1497. set_buffer_uptodate(nbh);
  1498. unlock_buffer(nbh);
  1499. mark_buffer_dirty_inode(nbh, inode);
  1500. aed = (struct allocExtDesc *)(nbh->b_data);
  1501. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  1502. aed->previousAllocExtLocation =
  1503. cpu_to_le32(obloc.logicalBlockNum);
  1504. if (epos->offset + adsize > inode->i_sb->s_blocksize) {
  1505. loffset = epos->offset;
  1506. aed->lengthAllocDescs = cpu_to_le32(adsize);
  1507. sptr = ptr - adsize;
  1508. dptr = nbh->b_data + sizeof(struct allocExtDesc);
  1509. memcpy(dptr, sptr, adsize);
  1510. epos->offset = sizeof(struct allocExtDesc) + adsize;
  1511. } else {
  1512. loffset = epos->offset + adsize;
  1513. aed->lengthAllocDescs = cpu_to_le32(0);
  1514. sptr = ptr;
  1515. epos->offset = sizeof(struct allocExtDesc);
  1516. if (epos->bh) {
  1517. aed = (struct allocExtDesc *)epos->bh->b_data;
  1518. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1519. } else {
  1520. iinfo->i_lenAlloc += adsize;
  1521. mark_inode_dirty(inode);
  1522. }
  1523. }
  1524. if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
  1525. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
  1526. epos->block.logicalBlockNum, sizeof(struct tag));
  1527. else
  1528. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
  1529. epos->block.logicalBlockNum, sizeof(struct tag));
  1530. switch (iinfo->i_alloc_type) {
  1531. case ICBTAG_FLAG_AD_SHORT:
  1532. sad = (struct short_ad *)sptr;
  1533. sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1534. inode->i_sb->s_blocksize);
  1535. sad->extPosition =
  1536. cpu_to_le32(epos->block.logicalBlockNum);
  1537. break;
  1538. case ICBTAG_FLAG_AD_LONG:
  1539. lad = (struct long_ad *)sptr;
  1540. lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1541. inode->i_sb->s_blocksize);
  1542. lad->extLocation = cpu_to_lelb(epos->block);
  1543. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1544. break;
  1545. }
  1546. if (epos->bh) {
  1547. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1548. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1549. udf_update_tag(epos->bh->b_data, loffset);
  1550. else
  1551. udf_update_tag(epos->bh->b_data,
  1552. sizeof(struct allocExtDesc));
  1553. mark_buffer_dirty_inode(epos->bh, inode);
  1554. brelse(epos->bh);
  1555. } else {
  1556. mark_inode_dirty(inode);
  1557. }
  1558. epos->bh = nbh;
  1559. }
  1560. etype = udf_write_aext(inode, epos, eloc, elen, inc);
  1561. if (!epos->bh) {
  1562. iinfo->i_lenAlloc += adsize;
  1563. mark_inode_dirty(inode);
  1564. } else {
  1565. aed = (struct allocExtDesc *)epos->bh->b_data;
  1566. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1567. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1568. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1569. udf_update_tag(epos->bh->b_data,
  1570. epos->offset + (inc ? 0 : adsize));
  1571. else
  1572. udf_update_tag(epos->bh->b_data,
  1573. sizeof(struct allocExtDesc));
  1574. mark_buffer_dirty_inode(epos->bh, inode);
  1575. }
  1576. return etype;
  1577. }
  1578. int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
  1579. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1580. {
  1581. int adsize;
  1582. uint8_t *ptr;
  1583. struct short_ad *sad;
  1584. struct long_ad *lad;
  1585. struct udf_inode_info *iinfo = UDF_I(inode);
  1586. if (!epos->bh)
  1587. ptr = iinfo->i_ext.i_data + epos->offset -
  1588. udf_file_entry_alloc_offset(inode) +
  1589. iinfo->i_lenEAttr;
  1590. else
  1591. ptr = epos->bh->b_data + epos->offset;
  1592. switch (iinfo->i_alloc_type) {
  1593. case ICBTAG_FLAG_AD_SHORT:
  1594. sad = (struct short_ad *)ptr;
  1595. sad->extLength = cpu_to_le32(elen);
  1596. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1597. adsize = sizeof(struct short_ad);
  1598. break;
  1599. case ICBTAG_FLAG_AD_LONG:
  1600. lad = (struct long_ad *)ptr;
  1601. lad->extLength = cpu_to_le32(elen);
  1602. lad->extLocation = cpu_to_lelb(*eloc);
  1603. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1604. adsize = sizeof(struct long_ad);
  1605. break;
  1606. default:
  1607. return -1;
  1608. }
  1609. if (epos->bh) {
  1610. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1611. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1612. struct allocExtDesc *aed =
  1613. (struct allocExtDesc *)epos->bh->b_data;
  1614. udf_update_tag(epos->bh->b_data,
  1615. le32_to_cpu(aed->lengthAllocDescs) +
  1616. sizeof(struct allocExtDesc));
  1617. }
  1618. mark_buffer_dirty_inode(epos->bh, inode);
  1619. } else {
  1620. mark_inode_dirty(inode);
  1621. }
  1622. if (inc)
  1623. epos->offset += adsize;
  1624. return (elen >> 30);
  1625. }
  1626. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1627. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1628. {
  1629. int8_t etype;
  1630. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1631. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1632. int block;
  1633. epos->block = *eloc;
  1634. epos->offset = sizeof(struct allocExtDesc);
  1635. brelse(epos->bh);
  1636. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1637. epos->bh = udf_tread(inode->i_sb, block);
  1638. if (!epos->bh) {
  1639. udf_debug("reading block %d failed!\n", block);
  1640. return -1;
  1641. }
  1642. }
  1643. return etype;
  1644. }
  1645. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1646. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1647. {
  1648. int alen;
  1649. int8_t etype;
  1650. uint8_t *ptr;
  1651. struct short_ad *sad;
  1652. struct long_ad *lad;
  1653. struct udf_inode_info *iinfo = UDF_I(inode);
  1654. if (!epos->bh) {
  1655. if (!epos->offset)
  1656. epos->offset = udf_file_entry_alloc_offset(inode);
  1657. ptr = iinfo->i_ext.i_data + epos->offset -
  1658. udf_file_entry_alloc_offset(inode) +
  1659. iinfo->i_lenEAttr;
  1660. alen = udf_file_entry_alloc_offset(inode) +
  1661. iinfo->i_lenAlloc;
  1662. } else {
  1663. if (!epos->offset)
  1664. epos->offset = sizeof(struct allocExtDesc);
  1665. ptr = epos->bh->b_data + epos->offset;
  1666. alen = sizeof(struct allocExtDesc) +
  1667. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1668. lengthAllocDescs);
  1669. }
  1670. switch (iinfo->i_alloc_type) {
  1671. case ICBTAG_FLAG_AD_SHORT:
  1672. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1673. if (!sad)
  1674. return -1;
  1675. etype = le32_to_cpu(sad->extLength) >> 30;
  1676. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1677. eloc->partitionReferenceNum =
  1678. iinfo->i_location.partitionReferenceNum;
  1679. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1680. break;
  1681. case ICBTAG_FLAG_AD_LONG:
  1682. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1683. if (!lad)
  1684. return -1;
  1685. etype = le32_to_cpu(lad->extLength) >> 30;
  1686. *eloc = lelb_to_cpu(lad->extLocation);
  1687. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1688. break;
  1689. default:
  1690. udf_debug("alloc_type = %d unsupported\n",
  1691. iinfo->i_alloc_type);
  1692. return -1;
  1693. }
  1694. return etype;
  1695. }
  1696. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1697. struct kernel_lb_addr neloc, uint32_t nelen)
  1698. {
  1699. struct kernel_lb_addr oeloc;
  1700. uint32_t oelen;
  1701. int8_t etype;
  1702. if (epos.bh)
  1703. get_bh(epos.bh);
  1704. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1705. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1706. neloc = oeloc;
  1707. nelen = (etype << 30) | oelen;
  1708. }
  1709. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1710. brelse(epos.bh);
  1711. return (nelen >> 30);
  1712. }
  1713. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1714. struct kernel_lb_addr eloc, uint32_t elen)
  1715. {
  1716. struct extent_position oepos;
  1717. int adsize;
  1718. int8_t etype;
  1719. struct allocExtDesc *aed;
  1720. struct udf_inode_info *iinfo;
  1721. if (epos.bh) {
  1722. get_bh(epos.bh);
  1723. get_bh(epos.bh);
  1724. }
  1725. iinfo = UDF_I(inode);
  1726. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1727. adsize = sizeof(struct short_ad);
  1728. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1729. adsize = sizeof(struct long_ad);
  1730. else
  1731. adsize = 0;
  1732. oepos = epos;
  1733. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1734. return -1;
  1735. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1736. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1737. if (oepos.bh != epos.bh) {
  1738. oepos.block = epos.block;
  1739. brelse(oepos.bh);
  1740. get_bh(epos.bh);
  1741. oepos.bh = epos.bh;
  1742. oepos.offset = epos.offset - adsize;
  1743. }
  1744. }
  1745. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1746. elen = 0;
  1747. if (epos.bh != oepos.bh) {
  1748. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1749. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1750. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1751. if (!oepos.bh) {
  1752. iinfo->i_lenAlloc -= (adsize * 2);
  1753. mark_inode_dirty(inode);
  1754. } else {
  1755. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1756. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1757. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1758. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1759. udf_update_tag(oepos.bh->b_data,
  1760. oepos.offset - (2 * adsize));
  1761. else
  1762. udf_update_tag(oepos.bh->b_data,
  1763. sizeof(struct allocExtDesc));
  1764. mark_buffer_dirty_inode(oepos.bh, inode);
  1765. }
  1766. } else {
  1767. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1768. if (!oepos.bh) {
  1769. iinfo->i_lenAlloc -= adsize;
  1770. mark_inode_dirty(inode);
  1771. } else {
  1772. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1773. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  1774. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1775. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1776. udf_update_tag(oepos.bh->b_data,
  1777. epos.offset - adsize);
  1778. else
  1779. udf_update_tag(oepos.bh->b_data,
  1780. sizeof(struct allocExtDesc));
  1781. mark_buffer_dirty_inode(oepos.bh, inode);
  1782. }
  1783. }
  1784. brelse(epos.bh);
  1785. brelse(oepos.bh);
  1786. return (elen >> 30);
  1787. }
  1788. int8_t inode_bmap(struct inode *inode, sector_t block,
  1789. struct extent_position *pos, struct kernel_lb_addr *eloc,
  1790. uint32_t *elen, sector_t *offset)
  1791. {
  1792. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1793. loff_t lbcount = 0, bcount =
  1794. (loff_t) block << blocksize_bits;
  1795. int8_t etype;
  1796. struct udf_inode_info *iinfo;
  1797. iinfo = UDF_I(inode);
  1798. pos->offset = 0;
  1799. pos->block = iinfo->i_location;
  1800. pos->bh = NULL;
  1801. *elen = 0;
  1802. do {
  1803. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  1804. if (etype == -1) {
  1805. *offset = (bcount - lbcount) >> blocksize_bits;
  1806. iinfo->i_lenExtents = lbcount;
  1807. return -1;
  1808. }
  1809. lbcount += *elen;
  1810. } while (lbcount <= bcount);
  1811. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  1812. return etype;
  1813. }
  1814. long udf_block_map(struct inode *inode, sector_t block)
  1815. {
  1816. struct kernel_lb_addr eloc;
  1817. uint32_t elen;
  1818. sector_t offset;
  1819. struct extent_position epos = {};
  1820. int ret;
  1821. lock_kernel();
  1822. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  1823. (EXT_RECORDED_ALLOCATED >> 30))
  1824. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  1825. else
  1826. ret = 0;
  1827. unlock_kernel();
  1828. brelse(epos.bh);
  1829. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  1830. return udf_fixed_to_variable(ret);
  1831. else
  1832. return ret;
  1833. }