inode.c 62 KB

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