inode.c 54 KB

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