inode.c 66 KB

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