inode.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109
  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 = NULL, *nbh = NULL;
  982. struct indirectEntry *ie;
  983. ibh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 1,
  984. &ident);
  985. if (ident == TAG_IDENT_IE) {
  986. if (ibh) {
  987. kernel_lb_addr loc;
  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. } else {
  1004. brelse(nbh);
  1005. brelse(ibh);
  1006. }
  1007. } else {
  1008. brelse(ibh);
  1009. }
  1010. }
  1011. } else {
  1012. brelse(ibh);
  1013. }
  1014. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1015. printk(KERN_ERR "udf: unsupported strategy type: %d\n",
  1016. le16_to_cpu(fe->icbTag.strategyType));
  1017. brelse(bh);
  1018. make_bad_inode(inode);
  1019. return;
  1020. }
  1021. udf_fill_inode(inode, bh);
  1022. brelse(bh);
  1023. }
  1024. static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
  1025. {
  1026. struct fileEntry *fe;
  1027. struct extendedFileEntry *efe;
  1028. time_t convtime;
  1029. long convtime_usec;
  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. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1108. lets_to_cpu(fe->accessTime))) {
  1109. inode->i_atime.tv_sec = convtime;
  1110. inode->i_atime.tv_nsec = convtime_usec * 1000;
  1111. } else {
  1112. inode->i_atime = sbi->s_record_time;
  1113. }
  1114. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1115. lets_to_cpu(fe->modificationTime))) {
  1116. inode->i_mtime.tv_sec = convtime;
  1117. inode->i_mtime.tv_nsec = convtime_usec * 1000;
  1118. } else {
  1119. inode->i_mtime = sbi->s_record_time;
  1120. }
  1121. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1122. lets_to_cpu(fe->attrTime))) {
  1123. inode->i_ctime.tv_sec = convtime;
  1124. inode->i_ctime.tv_nsec = convtime_usec * 1000;
  1125. } else {
  1126. inode->i_ctime = sbi->s_record_time;
  1127. }
  1128. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1129. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1130. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1131. offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
  1132. } else {
  1133. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1134. (inode->i_sb->s_blocksize_bits - 9);
  1135. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1136. lets_to_cpu(efe->accessTime))) {
  1137. inode->i_atime.tv_sec = convtime;
  1138. inode->i_atime.tv_nsec = convtime_usec * 1000;
  1139. } else {
  1140. inode->i_atime = sbi->s_record_time;
  1141. }
  1142. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1143. lets_to_cpu(efe->modificationTime))) {
  1144. inode->i_mtime.tv_sec = convtime;
  1145. inode->i_mtime.tv_nsec = convtime_usec * 1000;
  1146. } else {
  1147. inode->i_mtime = sbi->s_record_time;
  1148. }
  1149. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1150. lets_to_cpu(efe->createTime))) {
  1151. iinfo->i_crtime.tv_sec = convtime;
  1152. iinfo->i_crtime.tv_nsec = convtime_usec * 1000;
  1153. } else {
  1154. iinfo->i_crtime = sbi->s_record_time;
  1155. }
  1156. if (udf_stamp_to_time(&convtime, &convtime_usec,
  1157. lets_to_cpu(efe->attrTime))) {
  1158. inode->i_ctime.tv_sec = convtime;
  1159. inode->i_ctime.tv_nsec = convtime_usec * 1000;
  1160. } else {
  1161. inode->i_ctime = sbi->s_record_time;
  1162. }
  1163. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1164. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1165. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1166. offset = sizeof(struct extendedFileEntry) +
  1167. iinfo->i_lenEAttr;
  1168. }
  1169. switch (fe->icbTag.fileType) {
  1170. case ICBTAG_FILE_TYPE_DIRECTORY:
  1171. inode->i_op = &udf_dir_inode_operations;
  1172. inode->i_fop = &udf_dir_operations;
  1173. inode->i_mode |= S_IFDIR;
  1174. inc_nlink(inode);
  1175. break;
  1176. case ICBTAG_FILE_TYPE_REALTIME:
  1177. case ICBTAG_FILE_TYPE_REGULAR:
  1178. case ICBTAG_FILE_TYPE_UNDEF:
  1179. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1180. inode->i_data.a_ops = &udf_adinicb_aops;
  1181. else
  1182. inode->i_data.a_ops = &udf_aops;
  1183. inode->i_op = &udf_file_inode_operations;
  1184. inode->i_fop = &udf_file_operations;
  1185. inode->i_mode |= S_IFREG;
  1186. break;
  1187. case ICBTAG_FILE_TYPE_BLOCK:
  1188. inode->i_mode |= S_IFBLK;
  1189. break;
  1190. case ICBTAG_FILE_TYPE_CHAR:
  1191. inode->i_mode |= S_IFCHR;
  1192. break;
  1193. case ICBTAG_FILE_TYPE_FIFO:
  1194. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1195. break;
  1196. case ICBTAG_FILE_TYPE_SOCKET:
  1197. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1198. break;
  1199. case ICBTAG_FILE_TYPE_SYMLINK:
  1200. inode->i_data.a_ops = &udf_symlink_aops;
  1201. inode->i_op = &page_symlink_inode_operations;
  1202. inode->i_mode = S_IFLNK | S_IRWXUGO;
  1203. break;
  1204. default:
  1205. printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
  1206. "file type=%d\n", inode->i_ino,
  1207. fe->icbTag.fileType);
  1208. make_bad_inode(inode);
  1209. return;
  1210. }
  1211. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1212. struct deviceSpec *dsea =
  1213. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1214. if (dsea) {
  1215. init_special_inode(inode, inode->i_mode,
  1216. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1217. le32_to_cpu(dsea->minorDeviceIdent)));
  1218. /* Developer ID ??? */
  1219. } else
  1220. make_bad_inode(inode);
  1221. }
  1222. }
  1223. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1224. {
  1225. struct udf_inode_info *iinfo = UDF_I(inode);
  1226. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1227. if (!iinfo->i_ext.i_data) {
  1228. printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
  1229. "no free memory\n", inode->i_ino);
  1230. return -ENOMEM;
  1231. }
  1232. return 0;
  1233. }
  1234. static mode_t udf_convert_permissions(struct fileEntry *fe)
  1235. {
  1236. mode_t mode;
  1237. uint32_t permissions;
  1238. uint32_t flags;
  1239. permissions = le32_to_cpu(fe->permissions);
  1240. flags = le16_to_cpu(fe->icbTag.flags);
  1241. mode = ((permissions) & S_IRWXO) |
  1242. ((permissions >> 2) & S_IRWXG) |
  1243. ((permissions >> 4) & S_IRWXU) |
  1244. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1245. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1246. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1247. return mode;
  1248. }
  1249. int udf_write_inode(struct inode *inode, int sync)
  1250. {
  1251. int ret;
  1252. lock_kernel();
  1253. ret = udf_update_inode(inode, sync);
  1254. unlock_kernel();
  1255. return ret;
  1256. }
  1257. int udf_sync_inode(struct inode *inode)
  1258. {
  1259. return udf_update_inode(inode, 1);
  1260. }
  1261. static int udf_update_inode(struct inode *inode, int do_sync)
  1262. {
  1263. struct buffer_head *bh = NULL;
  1264. struct fileEntry *fe;
  1265. struct extendedFileEntry *efe;
  1266. uint32_t udfperms;
  1267. uint16_t icbflags;
  1268. uint16_t crclen;
  1269. kernel_timestamp cpu_time;
  1270. int err = 0;
  1271. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1272. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1273. struct udf_inode_info *iinfo = UDF_I(inode);
  1274. bh = udf_tread(inode->i_sb,
  1275. udf_get_lb_pblock(inode->i_sb,
  1276. iinfo->i_location, 0));
  1277. if (!bh) {
  1278. udf_debug("bread failure\n");
  1279. return -EIO;
  1280. }
  1281. memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
  1282. fe = (struct fileEntry *)bh->b_data;
  1283. efe = (struct extendedFileEntry *)bh->b_data;
  1284. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1285. struct unallocSpaceEntry *use =
  1286. (struct unallocSpaceEntry *)bh->b_data;
  1287. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1288. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1289. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1290. sizeof(struct unallocSpaceEntry));
  1291. crclen = sizeof(struct unallocSpaceEntry) +
  1292. iinfo->i_lenAlloc - sizeof(tag);
  1293. use->descTag.tagLocation = cpu_to_le32(
  1294. iinfo->i_location.
  1295. logicalBlockNum);
  1296. use->descTag.descCRCLength = cpu_to_le16(crclen);
  1297. use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
  1298. sizeof(tag), crclen,
  1299. 0));
  1300. use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
  1301. mark_buffer_dirty(bh);
  1302. brelse(bh);
  1303. return err;
  1304. }
  1305. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1306. fe->uid = cpu_to_le32(-1);
  1307. else
  1308. fe->uid = cpu_to_le32(inode->i_uid);
  1309. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1310. fe->gid = cpu_to_le32(-1);
  1311. else
  1312. fe->gid = cpu_to_le32(inode->i_gid);
  1313. udfperms = ((inode->i_mode & S_IRWXO)) |
  1314. ((inode->i_mode & S_IRWXG) << 2) |
  1315. ((inode->i_mode & S_IRWXU) << 4);
  1316. udfperms |= (le32_to_cpu(fe->permissions) &
  1317. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1318. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1319. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1320. fe->permissions = cpu_to_le32(udfperms);
  1321. if (S_ISDIR(inode->i_mode))
  1322. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1323. else
  1324. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1325. fe->informationLength = cpu_to_le64(inode->i_size);
  1326. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1327. regid *eid;
  1328. struct deviceSpec *dsea =
  1329. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1330. if (!dsea) {
  1331. dsea = (struct deviceSpec *)
  1332. udf_add_extendedattr(inode,
  1333. sizeof(struct deviceSpec) +
  1334. sizeof(regid), 12, 0x3);
  1335. dsea->attrType = cpu_to_le32(12);
  1336. dsea->attrSubtype = 1;
  1337. dsea->attrLength = cpu_to_le32(
  1338. sizeof(struct deviceSpec) +
  1339. sizeof(regid));
  1340. dsea->impUseLength = cpu_to_le32(sizeof(regid));
  1341. }
  1342. eid = (regid *)dsea->impUse;
  1343. memset(eid, 0, sizeof(regid));
  1344. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1345. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1346. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1347. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1348. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1349. }
  1350. if (iinfo->i_efe == 0) {
  1351. memcpy(bh->b_data + sizeof(struct fileEntry),
  1352. iinfo->i_ext.i_data,
  1353. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1354. fe->logicalBlocksRecorded = cpu_to_le64(
  1355. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1356. (blocksize_bits - 9));
  1357. if (udf_time_to_stamp(&cpu_time, inode->i_atime))
  1358. fe->accessTime = cpu_to_lets(cpu_time);
  1359. if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
  1360. fe->modificationTime = cpu_to_lets(cpu_time);
  1361. if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
  1362. fe->attrTime = cpu_to_lets(cpu_time);
  1363. memset(&(fe->impIdent), 0, sizeof(regid));
  1364. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1365. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1366. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1367. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1368. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1369. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1370. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1371. crclen = sizeof(struct fileEntry);
  1372. } else {
  1373. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1374. iinfo->i_ext.i_data,
  1375. inode->i_sb->s_blocksize -
  1376. sizeof(struct extendedFileEntry));
  1377. efe->objectSize = cpu_to_le64(inode->i_size);
  1378. efe->logicalBlocksRecorded = cpu_to_le64(
  1379. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1380. (blocksize_bits - 9));
  1381. if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
  1382. (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
  1383. iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
  1384. iinfo->i_crtime = inode->i_atime;
  1385. if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
  1386. (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
  1387. iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
  1388. iinfo->i_crtime = inode->i_mtime;
  1389. if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
  1390. (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
  1391. iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
  1392. iinfo->i_crtime = inode->i_ctime;
  1393. if (udf_time_to_stamp(&cpu_time, inode->i_atime))
  1394. efe->accessTime = cpu_to_lets(cpu_time);
  1395. if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
  1396. efe->modificationTime = cpu_to_lets(cpu_time);
  1397. if (udf_time_to_stamp(&cpu_time, iinfo->i_crtime))
  1398. efe->createTime = cpu_to_lets(cpu_time);
  1399. if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
  1400. efe->attrTime = cpu_to_lets(cpu_time);
  1401. memset(&(efe->impIdent), 0, sizeof(regid));
  1402. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1403. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1404. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1405. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1406. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1407. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1408. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1409. crclen = sizeof(struct extendedFileEntry);
  1410. }
  1411. if (iinfo->i_strat4096) {
  1412. fe->icbTag.strategyType = cpu_to_le16(4096);
  1413. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1414. fe->icbTag.numEntries = cpu_to_le16(2);
  1415. } else {
  1416. fe->icbTag.strategyType = cpu_to_le16(4);
  1417. fe->icbTag.numEntries = cpu_to_le16(1);
  1418. }
  1419. if (S_ISDIR(inode->i_mode))
  1420. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1421. else if (S_ISREG(inode->i_mode))
  1422. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1423. else if (S_ISLNK(inode->i_mode))
  1424. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1425. else if (S_ISBLK(inode->i_mode))
  1426. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1427. else if (S_ISCHR(inode->i_mode))
  1428. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1429. else if (S_ISFIFO(inode->i_mode))
  1430. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1431. else if (S_ISSOCK(inode->i_mode))
  1432. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1433. icbflags = iinfo->i_alloc_type |
  1434. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1435. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1436. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1437. (le16_to_cpu(fe->icbTag.flags) &
  1438. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1439. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1440. fe->icbTag.flags = cpu_to_le16(icbflags);
  1441. if (sbi->s_udfrev >= 0x0200)
  1442. fe->descTag.descVersion = cpu_to_le16(3);
  1443. else
  1444. fe->descTag.descVersion = cpu_to_le16(2);
  1445. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1446. fe->descTag.tagLocation = cpu_to_le32(
  1447. iinfo->i_location.logicalBlockNum);
  1448. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
  1449. sizeof(tag);
  1450. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1451. fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
  1452. crclen, 0));
  1453. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1454. /* write the data blocks */
  1455. mark_buffer_dirty(bh);
  1456. if (do_sync) {
  1457. sync_dirty_buffer(bh);
  1458. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  1459. printk(KERN_WARNING "IO error syncing udf inode "
  1460. "[%s:%08lx]\n", inode->i_sb->s_id,
  1461. inode->i_ino);
  1462. err = -EIO;
  1463. }
  1464. }
  1465. brelse(bh);
  1466. return err;
  1467. }
  1468. struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
  1469. {
  1470. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1471. struct inode *inode = iget_locked(sb, block);
  1472. if (!inode)
  1473. return NULL;
  1474. if (inode->i_state & I_NEW) {
  1475. memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
  1476. __udf_read_inode(inode);
  1477. unlock_new_inode(inode);
  1478. }
  1479. if (is_bad_inode(inode))
  1480. goto out_iput;
  1481. if (ino.logicalBlockNum >= UDF_SB(sb)->
  1482. s_partmaps[ino.partitionReferenceNum].s_partition_len) {
  1483. udf_debug("block=%d, partition=%d out of range\n",
  1484. ino.logicalBlockNum, ino.partitionReferenceNum);
  1485. make_bad_inode(inode);
  1486. goto out_iput;
  1487. }
  1488. return inode;
  1489. out_iput:
  1490. iput(inode);
  1491. return NULL;
  1492. }
  1493. int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
  1494. kernel_lb_addr eloc, uint32_t elen, int inc)
  1495. {
  1496. int adsize;
  1497. short_ad *sad = NULL;
  1498. long_ad *lad = NULL;
  1499. struct allocExtDesc *aed;
  1500. int8_t etype;
  1501. uint8_t *ptr;
  1502. struct udf_inode_info *iinfo = UDF_I(inode);
  1503. if (!epos->bh)
  1504. ptr = iinfo->i_ext.i_data + epos->offset -
  1505. udf_file_entry_alloc_offset(inode) +
  1506. iinfo->i_lenEAttr;
  1507. else
  1508. ptr = epos->bh->b_data + epos->offset;
  1509. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1510. adsize = sizeof(short_ad);
  1511. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1512. adsize = sizeof(long_ad);
  1513. else
  1514. return -1;
  1515. if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
  1516. char *sptr, *dptr;
  1517. struct buffer_head *nbh;
  1518. int err, loffset;
  1519. kernel_lb_addr obloc = epos->block;
  1520. epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
  1521. obloc.partitionReferenceNum,
  1522. obloc.logicalBlockNum, &err);
  1523. if (!epos->block.logicalBlockNum)
  1524. return -1;
  1525. nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
  1526. epos->block,
  1527. 0));
  1528. if (!nbh)
  1529. return -1;
  1530. lock_buffer(nbh);
  1531. memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
  1532. set_buffer_uptodate(nbh);
  1533. unlock_buffer(nbh);
  1534. mark_buffer_dirty_inode(nbh, inode);
  1535. aed = (struct allocExtDesc *)(nbh->b_data);
  1536. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
  1537. aed->previousAllocExtLocation =
  1538. cpu_to_le32(obloc.logicalBlockNum);
  1539. if (epos->offset + adsize > inode->i_sb->s_blocksize) {
  1540. loffset = epos->offset;
  1541. aed->lengthAllocDescs = cpu_to_le32(adsize);
  1542. sptr = ptr - adsize;
  1543. dptr = nbh->b_data + sizeof(struct allocExtDesc);
  1544. memcpy(dptr, sptr, adsize);
  1545. epos->offset = sizeof(struct allocExtDesc) + adsize;
  1546. } else {
  1547. loffset = epos->offset + adsize;
  1548. aed->lengthAllocDescs = cpu_to_le32(0);
  1549. sptr = ptr;
  1550. epos->offset = sizeof(struct allocExtDesc);
  1551. if (epos->bh) {
  1552. aed = (struct allocExtDesc *)epos->bh->b_data;
  1553. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1554. } else {
  1555. iinfo->i_lenAlloc += adsize;
  1556. mark_inode_dirty(inode);
  1557. }
  1558. }
  1559. if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
  1560. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
  1561. epos->block.logicalBlockNum, sizeof(tag));
  1562. else
  1563. udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
  1564. epos->block.logicalBlockNum, sizeof(tag));
  1565. switch (iinfo->i_alloc_type) {
  1566. case ICBTAG_FLAG_AD_SHORT:
  1567. sad = (short_ad *)sptr;
  1568. sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1569. inode->i_sb->s_blocksize);
  1570. sad->extPosition =
  1571. cpu_to_le32(epos->block.logicalBlockNum);
  1572. break;
  1573. case ICBTAG_FLAG_AD_LONG:
  1574. lad = (long_ad *)sptr;
  1575. lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
  1576. inode->i_sb->s_blocksize);
  1577. lad->extLocation = cpu_to_lelb(epos->block);
  1578. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1579. break;
  1580. }
  1581. if (epos->bh) {
  1582. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1583. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1584. udf_update_tag(epos->bh->b_data, loffset);
  1585. else
  1586. udf_update_tag(epos->bh->b_data,
  1587. sizeof(struct allocExtDesc));
  1588. mark_buffer_dirty_inode(epos->bh, inode);
  1589. brelse(epos->bh);
  1590. } else {
  1591. mark_inode_dirty(inode);
  1592. }
  1593. epos->bh = nbh;
  1594. }
  1595. etype = udf_write_aext(inode, epos, eloc, elen, inc);
  1596. if (!epos->bh) {
  1597. iinfo->i_lenAlloc += adsize;
  1598. mark_inode_dirty(inode);
  1599. } else {
  1600. aed = (struct allocExtDesc *)epos->bh->b_data;
  1601. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1602. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1603. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1604. udf_update_tag(epos->bh->b_data,
  1605. epos->offset + (inc ? 0 : adsize));
  1606. else
  1607. udf_update_tag(epos->bh->b_data,
  1608. sizeof(struct allocExtDesc));
  1609. mark_buffer_dirty_inode(epos->bh, inode);
  1610. }
  1611. return etype;
  1612. }
  1613. int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
  1614. kernel_lb_addr eloc, uint32_t elen, int inc)
  1615. {
  1616. int adsize;
  1617. uint8_t *ptr;
  1618. short_ad *sad;
  1619. long_ad *lad;
  1620. struct udf_inode_info *iinfo = UDF_I(inode);
  1621. if (!epos->bh)
  1622. ptr = iinfo->i_ext.i_data + epos->offset -
  1623. udf_file_entry_alloc_offset(inode) +
  1624. iinfo->i_lenEAttr;
  1625. else
  1626. ptr = epos->bh->b_data + epos->offset;
  1627. switch (iinfo->i_alloc_type) {
  1628. case ICBTAG_FLAG_AD_SHORT:
  1629. sad = (short_ad *)ptr;
  1630. sad->extLength = cpu_to_le32(elen);
  1631. sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
  1632. adsize = sizeof(short_ad);
  1633. break;
  1634. case ICBTAG_FLAG_AD_LONG:
  1635. lad = (long_ad *)ptr;
  1636. lad->extLength = cpu_to_le32(elen);
  1637. lad->extLocation = cpu_to_lelb(eloc);
  1638. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1639. adsize = sizeof(long_ad);
  1640. break;
  1641. default:
  1642. return -1;
  1643. }
  1644. if (epos->bh) {
  1645. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1646. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1647. struct allocExtDesc *aed =
  1648. (struct allocExtDesc *)epos->bh->b_data;
  1649. udf_update_tag(epos->bh->b_data,
  1650. le32_to_cpu(aed->lengthAllocDescs) +
  1651. sizeof(struct allocExtDesc));
  1652. }
  1653. mark_buffer_dirty_inode(epos->bh, inode);
  1654. } else {
  1655. mark_inode_dirty(inode);
  1656. }
  1657. if (inc)
  1658. epos->offset += adsize;
  1659. return (elen >> 30);
  1660. }
  1661. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1662. kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1663. {
  1664. int8_t etype;
  1665. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1666. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1667. int block;
  1668. epos->block = *eloc;
  1669. epos->offset = sizeof(struct allocExtDesc);
  1670. brelse(epos->bh);
  1671. block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
  1672. epos->bh = udf_tread(inode->i_sb, block);
  1673. if (!epos->bh) {
  1674. udf_debug("reading block %d failed!\n", block);
  1675. return -1;
  1676. }
  1677. }
  1678. return etype;
  1679. }
  1680. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1681. kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1682. {
  1683. int alen;
  1684. int8_t etype;
  1685. uint8_t *ptr;
  1686. short_ad *sad;
  1687. long_ad *lad;
  1688. struct udf_inode_info *iinfo = UDF_I(inode);
  1689. if (!epos->bh) {
  1690. if (!epos->offset)
  1691. epos->offset = udf_file_entry_alloc_offset(inode);
  1692. ptr = iinfo->i_ext.i_data + epos->offset -
  1693. udf_file_entry_alloc_offset(inode) +
  1694. iinfo->i_lenEAttr;
  1695. alen = udf_file_entry_alloc_offset(inode) +
  1696. iinfo->i_lenAlloc;
  1697. } else {
  1698. if (!epos->offset)
  1699. epos->offset = sizeof(struct allocExtDesc);
  1700. ptr = epos->bh->b_data + epos->offset;
  1701. alen = sizeof(struct allocExtDesc) +
  1702. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1703. lengthAllocDescs);
  1704. }
  1705. switch (iinfo->i_alloc_type) {
  1706. case ICBTAG_FLAG_AD_SHORT:
  1707. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1708. if (!sad)
  1709. return -1;
  1710. etype = le32_to_cpu(sad->extLength) >> 30;
  1711. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1712. eloc->partitionReferenceNum =
  1713. iinfo->i_location.partitionReferenceNum;
  1714. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1715. break;
  1716. case ICBTAG_FLAG_AD_LONG:
  1717. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1718. if (!lad)
  1719. return -1;
  1720. etype = le32_to_cpu(lad->extLength) >> 30;
  1721. *eloc = lelb_to_cpu(lad->extLocation);
  1722. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1723. break;
  1724. default:
  1725. udf_debug("alloc_type = %d unsupported\n",
  1726. iinfo->i_alloc_type);
  1727. return -1;
  1728. }
  1729. return etype;
  1730. }
  1731. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1732. kernel_lb_addr neloc, uint32_t nelen)
  1733. {
  1734. kernel_lb_addr oeloc;
  1735. uint32_t oelen;
  1736. int8_t etype;
  1737. if (epos.bh)
  1738. get_bh(epos.bh);
  1739. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1740. udf_write_aext(inode, &epos, neloc, nelen, 1);
  1741. neloc = oeloc;
  1742. nelen = (etype << 30) | oelen;
  1743. }
  1744. udf_add_aext(inode, &epos, neloc, nelen, 1);
  1745. brelse(epos.bh);
  1746. return (nelen >> 30);
  1747. }
  1748. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1749. kernel_lb_addr eloc, uint32_t elen)
  1750. {
  1751. struct extent_position oepos;
  1752. int adsize;
  1753. int8_t etype;
  1754. struct allocExtDesc *aed;
  1755. struct udf_inode_info *iinfo;
  1756. if (epos.bh) {
  1757. get_bh(epos.bh);
  1758. get_bh(epos.bh);
  1759. }
  1760. iinfo = UDF_I(inode);
  1761. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1762. adsize = sizeof(short_ad);
  1763. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1764. adsize = sizeof(long_ad);
  1765. else
  1766. adsize = 0;
  1767. oepos = epos;
  1768. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1769. return -1;
  1770. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1771. udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
  1772. if (oepos.bh != epos.bh) {
  1773. oepos.block = epos.block;
  1774. brelse(oepos.bh);
  1775. get_bh(epos.bh);
  1776. oepos.bh = epos.bh;
  1777. oepos.offset = epos.offset - adsize;
  1778. }
  1779. }
  1780. memset(&eloc, 0x00, sizeof(kernel_lb_addr));
  1781. elen = 0;
  1782. if (epos.bh != oepos.bh) {
  1783. udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
  1784. udf_write_aext(inode, &oepos, eloc, elen, 1);
  1785. udf_write_aext(inode, &oepos, eloc, elen, 1);
  1786. if (!oepos.bh) {
  1787. iinfo->i_lenAlloc -= (adsize * 2);
  1788. mark_inode_dirty(inode);
  1789. } else {
  1790. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1791. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1792. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1793. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1794. udf_update_tag(oepos.bh->b_data,
  1795. oepos.offset - (2 * adsize));
  1796. else
  1797. udf_update_tag(oepos.bh->b_data,
  1798. sizeof(struct allocExtDesc));
  1799. mark_buffer_dirty_inode(oepos.bh, inode);
  1800. }
  1801. } else {
  1802. udf_write_aext(inode, &oepos, eloc, elen, 1);
  1803. if (!oepos.bh) {
  1804. iinfo->i_lenAlloc -= adsize;
  1805. mark_inode_dirty(inode);
  1806. } else {
  1807. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1808. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  1809. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1810. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1811. udf_update_tag(oepos.bh->b_data,
  1812. epos.offset - adsize);
  1813. else
  1814. udf_update_tag(oepos.bh->b_data,
  1815. sizeof(struct allocExtDesc));
  1816. mark_buffer_dirty_inode(oepos.bh, inode);
  1817. }
  1818. }
  1819. brelse(epos.bh);
  1820. brelse(oepos.bh);
  1821. return (elen >> 30);
  1822. }
  1823. int8_t inode_bmap(struct inode *inode, sector_t block,
  1824. struct extent_position *pos, kernel_lb_addr *eloc,
  1825. uint32_t *elen, sector_t *offset)
  1826. {
  1827. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1828. loff_t lbcount = 0, bcount =
  1829. (loff_t) block << blocksize_bits;
  1830. int8_t etype;
  1831. struct udf_inode_info *iinfo;
  1832. if (block < 0) {
  1833. printk(KERN_ERR "udf: inode_bmap: block < 0\n");
  1834. return -1;
  1835. }
  1836. iinfo = UDF_I(inode);
  1837. pos->offset = 0;
  1838. pos->block = iinfo->i_location;
  1839. pos->bh = NULL;
  1840. *elen = 0;
  1841. do {
  1842. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  1843. if (etype == -1) {
  1844. *offset = (bcount - lbcount) >> blocksize_bits;
  1845. iinfo->i_lenExtents = lbcount;
  1846. return -1;
  1847. }
  1848. lbcount += *elen;
  1849. } while (lbcount <= bcount);
  1850. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  1851. return etype;
  1852. }
  1853. long udf_block_map(struct inode *inode, sector_t block)
  1854. {
  1855. kernel_lb_addr eloc;
  1856. uint32_t elen;
  1857. sector_t offset;
  1858. struct extent_position epos = {};
  1859. int ret;
  1860. lock_kernel();
  1861. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  1862. (EXT_RECORDED_ALLOCATED >> 30))
  1863. ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
  1864. else
  1865. ret = 0;
  1866. unlock_kernel();
  1867. brelse(epos.bh);
  1868. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  1869. return udf_fixed_to_variable(ret);
  1870. else
  1871. return ret;
  1872. }