inode.c 57 KB

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