inode.c 61 KB

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