inode.c 58 KB

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