inode.c 62 KB

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