inode.c 59 KB

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