inode.c 58 KB

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