inode.c 65 KB

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