inode.c 66 KB

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