inode.c 54 KB

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