inode.c 68 KB

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