inode.c 66 KB

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