inode.c 65 KB

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