inode.c 57 KB

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