inode.c 66 KB

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