inode.c 58 KB

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