inode.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317
  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 by 'blocks' blocks, return the number of extents added */
  424. static int udf_do_extend_file(struct inode *inode,
  425. struct extent_position *last_pos,
  426. struct kernel_long_ad *last_ext,
  427. sector_t blocks)
  428. {
  429. sector_t add;
  430. int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  431. struct super_block *sb = inode->i_sb;
  432. struct kernel_lb_addr prealloc_loc = {};
  433. uint32_t prealloc_len = 0;
  434. struct udf_inode_info *iinfo;
  435. int err;
  436. /* The previous extent is fake and we should not extend by anything
  437. * - there's nothing to do... */
  438. if (!blocks && fake)
  439. return 0;
  440. iinfo = UDF_I(inode);
  441. /* Round the last extent up to a multiple of block size */
  442. if (last_ext->extLength & (sb->s_blocksize - 1)) {
  443. last_ext->extLength =
  444. (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
  445. (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
  446. sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
  447. iinfo->i_lenExtents =
  448. (iinfo->i_lenExtents + sb->s_blocksize - 1) &
  449. ~(sb->s_blocksize - 1);
  450. }
  451. /* Last extent are just preallocated blocks? */
  452. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  453. EXT_NOT_RECORDED_ALLOCATED) {
  454. /* Save the extent so that we can reattach it to the end */
  455. prealloc_loc = last_ext->extLocation;
  456. prealloc_len = last_ext->extLength;
  457. /* Mark the extent as a hole */
  458. last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  459. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
  460. last_ext->extLocation.logicalBlockNum = 0;
  461. last_ext->extLocation.partitionReferenceNum = 0;
  462. }
  463. /* Can we merge with the previous extent? */
  464. if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
  465. EXT_NOT_RECORDED_NOT_ALLOCATED) {
  466. add = ((1 << 30) - sb->s_blocksize -
  467. (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
  468. sb->s_blocksize_bits;
  469. if (add > blocks)
  470. add = blocks;
  471. blocks -= add;
  472. last_ext->extLength += add << sb->s_blocksize_bits;
  473. }
  474. if (fake) {
  475. udf_add_aext(inode, last_pos, &last_ext->extLocation,
  476. last_ext->extLength, 1);
  477. count++;
  478. } else {
  479. struct kernel_lb_addr tmploc;
  480. uint32_t tmplen;
  481. udf_write_aext(inode, last_pos, &last_ext->extLocation,
  482. last_ext->extLength, 1);
  483. /*
  484. * We've rewritten the last extent but there may be empty
  485. * indirect extent after it - enter it.
  486. */
  487. udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
  488. }
  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. udf_pblk_t newblocknum, newblock;
  598. sector_t offset = 0;
  599. int8_t etype;
  600. struct udf_inode_info *iinfo = UDF_I(inode);
  601. udf_pblk_t 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. newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  660. goto out_free;
  661. }
  662. /* Are we beyond EOF? */
  663. if (etype == -1) {
  664. int ret;
  665. isBeyondEOF = true;
  666. if (count) {
  667. if (c)
  668. laarr[0] = laarr[1];
  669. startnum = 1;
  670. } else {
  671. /* Create a fake extent when there's not one */
  672. memset(&laarr[0].extLocation, 0x00,
  673. sizeof(struct kernel_lb_addr));
  674. laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
  675. /* Will udf_do_extend_file() create real extent from
  676. a fake one? */
  677. startnum = (offset > 0);
  678. }
  679. /* Create extents for the hole between EOF and offset */
  680. ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
  681. if (ret < 0) {
  682. *err = ret;
  683. newblock = 0;
  684. goto out_free;
  685. }
  686. c = 0;
  687. offset = 0;
  688. count += ret;
  689. /* We are not covered by a preallocated extent? */
  690. if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
  691. EXT_NOT_RECORDED_ALLOCATED) {
  692. /* Is there any real extent? - otherwise we overwrite
  693. * the fake one... */
  694. if (count)
  695. c = !c;
  696. laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
  697. inode->i_sb->s_blocksize;
  698. memset(&laarr[c].extLocation, 0x00,
  699. sizeof(struct kernel_lb_addr));
  700. count++;
  701. }
  702. endnum = c + 1;
  703. lastblock = 1;
  704. } else {
  705. isBeyondEOF = false;
  706. endnum = startnum = ((count > 2) ? 2 : count);
  707. /* if the current extent is in position 0,
  708. swap it with the previous */
  709. if (!c && count != 1) {
  710. laarr[2] = laarr[0];
  711. laarr[0] = laarr[1];
  712. laarr[1] = laarr[2];
  713. c = 1;
  714. }
  715. /* if the current block is located in an extent,
  716. read the next extent */
  717. etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
  718. if (etype != -1) {
  719. laarr[c + 1].extLength = (etype << 30) | elen;
  720. laarr[c + 1].extLocation = eloc;
  721. count++;
  722. startnum++;
  723. endnum++;
  724. } else
  725. lastblock = 1;
  726. }
  727. /* if the current extent is not recorded but allocated, get the
  728. * block in the extent corresponding to the requested block */
  729. if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  730. newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
  731. else { /* otherwise, allocate a new block */
  732. if (iinfo->i_next_alloc_block == block)
  733. goal = iinfo->i_next_alloc_goal;
  734. if (!goal) {
  735. if (!(goal = pgoal)) /* XXX: what was intended here? */
  736. goal = iinfo->i_location.logicalBlockNum + 1;
  737. }
  738. newblocknum = udf_new_block(inode->i_sb, inode,
  739. iinfo->i_location.partitionReferenceNum,
  740. goal, err);
  741. if (!newblocknum) {
  742. *err = -ENOSPC;
  743. newblock = 0;
  744. goto out_free;
  745. }
  746. if (isBeyondEOF)
  747. iinfo->i_lenExtents += inode->i_sb->s_blocksize;
  748. }
  749. /* if the extent the requsted block is located in contains multiple
  750. * blocks, split the extent into at most three extents. blocks prior
  751. * to requested block, requested block, and blocks after requested
  752. * block */
  753. udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
  754. /* We preallocate blocks only for regular files. It also makes sense
  755. * for directories but there's a problem when to drop the
  756. * preallocation. We might use some delayed work for that but I feel
  757. * it's overengineering for a filesystem like UDF. */
  758. if (S_ISREG(inode->i_mode))
  759. udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
  760. /* merge any continuous blocks in laarr */
  761. udf_merge_extents(inode, laarr, &endnum);
  762. /* write back the new extents, inserting new extents if the new number
  763. * of extents is greater than the old number, and deleting extents if
  764. * the new number of extents is less than the old number */
  765. udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
  766. newblock = udf_get_pblock(inode->i_sb, newblocknum,
  767. iinfo->i_location.partitionReferenceNum, 0);
  768. if (!newblock) {
  769. *err = -EIO;
  770. goto out_free;
  771. }
  772. *new = 1;
  773. iinfo->i_next_alloc_block = block;
  774. iinfo->i_next_alloc_goal = newblocknum;
  775. inode->i_ctime = current_time(inode);
  776. if (IS_SYNC(inode))
  777. udf_sync_inode(inode);
  778. else
  779. mark_inode_dirty(inode);
  780. out_free:
  781. brelse(prev_epos.bh);
  782. brelse(cur_epos.bh);
  783. brelse(next_epos.bh);
  784. return newblock;
  785. }
  786. static void udf_split_extents(struct inode *inode, int *c, int offset,
  787. udf_pblk_t newblocknum,
  788. struct kernel_long_ad *laarr, int *endnum)
  789. {
  790. unsigned long blocksize = inode->i_sb->s_blocksize;
  791. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  792. if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
  793. (laarr[*c].extLength >> 30) ==
  794. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  795. int curr = *c;
  796. int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
  797. blocksize - 1) >> blocksize_bits;
  798. int8_t etype = (laarr[curr].extLength >> 30);
  799. if (blen == 1)
  800. ;
  801. else if (!offset || blen == offset + 1) {
  802. laarr[curr + 2] = laarr[curr + 1];
  803. laarr[curr + 1] = laarr[curr];
  804. } else {
  805. laarr[curr + 3] = laarr[curr + 1];
  806. laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
  807. }
  808. if (offset) {
  809. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  810. udf_free_blocks(inode->i_sb, inode,
  811. &laarr[curr].extLocation,
  812. 0, offset);
  813. laarr[curr].extLength =
  814. EXT_NOT_RECORDED_NOT_ALLOCATED |
  815. (offset << blocksize_bits);
  816. laarr[curr].extLocation.logicalBlockNum = 0;
  817. laarr[curr].extLocation.
  818. partitionReferenceNum = 0;
  819. } else
  820. laarr[curr].extLength = (etype << 30) |
  821. (offset << blocksize_bits);
  822. curr++;
  823. (*c)++;
  824. (*endnum)++;
  825. }
  826. laarr[curr].extLocation.logicalBlockNum = newblocknum;
  827. if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
  828. laarr[curr].extLocation.partitionReferenceNum =
  829. UDF_I(inode)->i_location.partitionReferenceNum;
  830. laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
  831. blocksize;
  832. curr++;
  833. if (blen != offset + 1) {
  834. if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
  835. laarr[curr].extLocation.logicalBlockNum +=
  836. offset + 1;
  837. laarr[curr].extLength = (etype << 30) |
  838. ((blen - (offset + 1)) << blocksize_bits);
  839. curr++;
  840. (*endnum)++;
  841. }
  842. }
  843. }
  844. static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
  845. struct kernel_long_ad *laarr,
  846. int *endnum)
  847. {
  848. int start, length = 0, currlength = 0, i;
  849. if (*endnum >= (c + 1)) {
  850. if (!lastblock)
  851. return;
  852. else
  853. start = c;
  854. } else {
  855. if ((laarr[c + 1].extLength >> 30) ==
  856. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  857. start = c + 1;
  858. length = currlength =
  859. (((laarr[c + 1].extLength &
  860. UDF_EXTENT_LENGTH_MASK) +
  861. inode->i_sb->s_blocksize - 1) >>
  862. inode->i_sb->s_blocksize_bits);
  863. } else
  864. start = c;
  865. }
  866. for (i = start + 1; i <= *endnum; i++) {
  867. if (i == *endnum) {
  868. if (lastblock)
  869. length += UDF_DEFAULT_PREALLOC_BLOCKS;
  870. } else if ((laarr[i].extLength >> 30) ==
  871. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
  872. length += (((laarr[i].extLength &
  873. UDF_EXTENT_LENGTH_MASK) +
  874. inode->i_sb->s_blocksize - 1) >>
  875. inode->i_sb->s_blocksize_bits);
  876. } else
  877. break;
  878. }
  879. if (length) {
  880. int next = laarr[start].extLocation.logicalBlockNum +
  881. (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
  882. inode->i_sb->s_blocksize - 1) >>
  883. inode->i_sb->s_blocksize_bits);
  884. int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
  885. laarr[start].extLocation.partitionReferenceNum,
  886. next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
  887. length : UDF_DEFAULT_PREALLOC_BLOCKS) -
  888. currlength);
  889. if (numalloc) {
  890. if (start == (c + 1))
  891. laarr[start].extLength +=
  892. (numalloc <<
  893. inode->i_sb->s_blocksize_bits);
  894. else {
  895. memmove(&laarr[c + 2], &laarr[c + 1],
  896. sizeof(struct long_ad) * (*endnum - (c + 1)));
  897. (*endnum)++;
  898. laarr[c + 1].extLocation.logicalBlockNum = next;
  899. laarr[c + 1].extLocation.partitionReferenceNum =
  900. laarr[c].extLocation.
  901. partitionReferenceNum;
  902. laarr[c + 1].extLength =
  903. EXT_NOT_RECORDED_ALLOCATED |
  904. (numalloc <<
  905. inode->i_sb->s_blocksize_bits);
  906. start = c + 1;
  907. }
  908. for (i = start + 1; numalloc && i < *endnum; i++) {
  909. int elen = ((laarr[i].extLength &
  910. UDF_EXTENT_LENGTH_MASK) +
  911. inode->i_sb->s_blocksize - 1) >>
  912. inode->i_sb->s_blocksize_bits;
  913. if (elen > numalloc) {
  914. laarr[i].extLength -=
  915. (numalloc <<
  916. inode->i_sb->s_blocksize_bits);
  917. numalloc = 0;
  918. } else {
  919. numalloc -= elen;
  920. if (*endnum > (i + 1))
  921. memmove(&laarr[i],
  922. &laarr[i + 1],
  923. sizeof(struct long_ad) *
  924. (*endnum - (i + 1)));
  925. i--;
  926. (*endnum)--;
  927. }
  928. }
  929. UDF_I(inode)->i_lenExtents +=
  930. numalloc << inode->i_sb->s_blocksize_bits;
  931. }
  932. }
  933. }
  934. static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
  935. int *endnum)
  936. {
  937. int i;
  938. unsigned long blocksize = inode->i_sb->s_blocksize;
  939. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  940. for (i = 0; i < (*endnum - 1); i++) {
  941. struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
  942. struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
  943. if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
  944. (((li->extLength >> 30) ==
  945. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
  946. ((lip1->extLocation.logicalBlockNum -
  947. li->extLocation.logicalBlockNum) ==
  948. (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  949. blocksize - 1) >> blocksize_bits)))) {
  950. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  951. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  952. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  953. lip1->extLength = (lip1->extLength -
  954. (li->extLength &
  955. UDF_EXTENT_LENGTH_MASK) +
  956. UDF_EXTENT_LENGTH_MASK) &
  957. ~(blocksize - 1);
  958. li->extLength = (li->extLength &
  959. UDF_EXTENT_FLAG_MASK) +
  960. (UDF_EXTENT_LENGTH_MASK + 1) -
  961. blocksize;
  962. lip1->extLocation.logicalBlockNum =
  963. li->extLocation.logicalBlockNum +
  964. ((li->extLength &
  965. UDF_EXTENT_LENGTH_MASK) >>
  966. blocksize_bits);
  967. } else {
  968. li->extLength = lip1->extLength +
  969. (((li->extLength &
  970. UDF_EXTENT_LENGTH_MASK) +
  971. blocksize - 1) & ~(blocksize - 1));
  972. if (*endnum > (i + 2))
  973. memmove(&laarr[i + 1], &laarr[i + 2],
  974. sizeof(struct long_ad) *
  975. (*endnum - (i + 2)));
  976. i--;
  977. (*endnum)--;
  978. }
  979. } else if (((li->extLength >> 30) ==
  980. (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
  981. ((lip1->extLength >> 30) ==
  982. (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
  983. udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
  984. ((li->extLength &
  985. UDF_EXTENT_LENGTH_MASK) +
  986. blocksize - 1) >> blocksize_bits);
  987. li->extLocation.logicalBlockNum = 0;
  988. li->extLocation.partitionReferenceNum = 0;
  989. if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
  990. (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
  991. blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
  992. lip1->extLength = (lip1->extLength -
  993. (li->extLength &
  994. UDF_EXTENT_LENGTH_MASK) +
  995. UDF_EXTENT_LENGTH_MASK) &
  996. ~(blocksize - 1);
  997. li->extLength = (li->extLength &
  998. UDF_EXTENT_FLAG_MASK) +
  999. (UDF_EXTENT_LENGTH_MASK + 1) -
  1000. blocksize;
  1001. } else {
  1002. li->extLength = lip1->extLength +
  1003. (((li->extLength &
  1004. UDF_EXTENT_LENGTH_MASK) +
  1005. blocksize - 1) & ~(blocksize - 1));
  1006. if (*endnum > (i + 2))
  1007. memmove(&laarr[i + 1], &laarr[i + 2],
  1008. sizeof(struct long_ad) *
  1009. (*endnum - (i + 2)));
  1010. i--;
  1011. (*endnum)--;
  1012. }
  1013. } else if ((li->extLength >> 30) ==
  1014. (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
  1015. udf_free_blocks(inode->i_sb, inode,
  1016. &li->extLocation, 0,
  1017. ((li->extLength &
  1018. UDF_EXTENT_LENGTH_MASK) +
  1019. blocksize - 1) >> blocksize_bits);
  1020. li->extLocation.logicalBlockNum = 0;
  1021. li->extLocation.partitionReferenceNum = 0;
  1022. li->extLength = (li->extLength &
  1023. UDF_EXTENT_LENGTH_MASK) |
  1024. EXT_NOT_RECORDED_NOT_ALLOCATED;
  1025. }
  1026. }
  1027. }
  1028. static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
  1029. int startnum, int endnum,
  1030. struct extent_position *epos)
  1031. {
  1032. int start = 0, i;
  1033. struct kernel_lb_addr tmploc;
  1034. uint32_t tmplen;
  1035. if (startnum > endnum) {
  1036. for (i = 0; i < (startnum - endnum); i++)
  1037. udf_delete_aext(inode, *epos, laarr[i].extLocation,
  1038. laarr[i].extLength);
  1039. } else if (startnum < endnum) {
  1040. for (i = 0; i < (endnum - startnum); i++) {
  1041. udf_insert_aext(inode, *epos, laarr[i].extLocation,
  1042. laarr[i].extLength);
  1043. udf_next_aext(inode, epos, &laarr[i].extLocation,
  1044. &laarr[i].extLength, 1);
  1045. start++;
  1046. }
  1047. }
  1048. for (i = start; i < endnum; i++) {
  1049. udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
  1050. udf_write_aext(inode, epos, &laarr[i].extLocation,
  1051. laarr[i].extLength, 1);
  1052. }
  1053. }
  1054. struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
  1055. int create, int *err)
  1056. {
  1057. struct buffer_head *bh = NULL;
  1058. bh = udf_getblk(inode, block, create, err);
  1059. if (!bh)
  1060. return NULL;
  1061. if (buffer_uptodate(bh))
  1062. return bh;
  1063. ll_rw_block(REQ_OP_READ, 0, 1, &bh);
  1064. wait_on_buffer(bh);
  1065. if (buffer_uptodate(bh))
  1066. return bh;
  1067. brelse(bh);
  1068. *err = -EIO;
  1069. return NULL;
  1070. }
  1071. int udf_setsize(struct inode *inode, loff_t newsize)
  1072. {
  1073. int err;
  1074. struct udf_inode_info *iinfo;
  1075. unsigned int bsize = i_blocksize(inode);
  1076. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  1077. S_ISLNK(inode->i_mode)))
  1078. return -EINVAL;
  1079. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1080. return -EPERM;
  1081. iinfo = UDF_I(inode);
  1082. if (newsize > inode->i_size) {
  1083. down_write(&iinfo->i_data_sem);
  1084. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1085. if (bsize <
  1086. (udf_file_entry_alloc_offset(inode) + newsize)) {
  1087. err = udf_expand_file_adinicb(inode);
  1088. if (err)
  1089. return err;
  1090. down_write(&iinfo->i_data_sem);
  1091. } else {
  1092. iinfo->i_lenAlloc = newsize;
  1093. goto set_size;
  1094. }
  1095. }
  1096. err = udf_extend_file(inode, newsize);
  1097. if (err) {
  1098. up_write(&iinfo->i_data_sem);
  1099. return err;
  1100. }
  1101. set_size:
  1102. up_write(&iinfo->i_data_sem);
  1103. truncate_setsize(inode, newsize);
  1104. } else {
  1105. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1106. down_write(&iinfo->i_data_sem);
  1107. udf_clear_extent_cache(inode);
  1108. memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
  1109. 0x00, bsize - newsize -
  1110. udf_file_entry_alloc_offset(inode));
  1111. iinfo->i_lenAlloc = newsize;
  1112. truncate_setsize(inode, newsize);
  1113. up_write(&iinfo->i_data_sem);
  1114. goto update_time;
  1115. }
  1116. err = block_truncate_page(inode->i_mapping, newsize,
  1117. udf_get_block);
  1118. if (err)
  1119. return err;
  1120. truncate_setsize(inode, newsize);
  1121. down_write(&iinfo->i_data_sem);
  1122. udf_clear_extent_cache(inode);
  1123. udf_truncate_extents(inode);
  1124. up_write(&iinfo->i_data_sem);
  1125. }
  1126. update_time:
  1127. inode->i_mtime = inode->i_ctime = current_time(inode);
  1128. if (IS_SYNC(inode))
  1129. udf_sync_inode(inode);
  1130. else
  1131. mark_inode_dirty(inode);
  1132. return 0;
  1133. }
  1134. /*
  1135. * Maximum length of linked list formed by ICB hierarchy. The chosen number is
  1136. * arbitrary - just that we hopefully don't limit any real use of rewritten
  1137. * inode on write-once media but avoid looping for too long on corrupted media.
  1138. */
  1139. #define UDF_MAX_ICB_NESTING 1024
  1140. static int udf_read_inode(struct inode *inode, bool hidden_inode)
  1141. {
  1142. struct buffer_head *bh = NULL;
  1143. struct fileEntry *fe;
  1144. struct extendedFileEntry *efe;
  1145. uint16_t ident;
  1146. struct udf_inode_info *iinfo = UDF_I(inode);
  1147. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1148. struct kernel_lb_addr *iloc = &iinfo->i_location;
  1149. struct timespec ts;
  1150. unsigned int link_count;
  1151. unsigned int indirections = 0;
  1152. int bs = inode->i_sb->s_blocksize;
  1153. int ret = -EIO;
  1154. uint32_t uid, gid;
  1155. reread:
  1156. if (iloc->partitionReferenceNum >= sbi->s_partitions) {
  1157. udf_debug("partition reference: %u > logical volume partitions: %u\n",
  1158. iloc->partitionReferenceNum, sbi->s_partitions);
  1159. return -EIO;
  1160. }
  1161. if (iloc->logicalBlockNum >=
  1162. sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
  1163. udf_debug("block=%u, partition=%u out of range\n",
  1164. iloc->logicalBlockNum, iloc->partitionReferenceNum);
  1165. return -EIO;
  1166. }
  1167. /*
  1168. * Set defaults, but the inode is still incomplete!
  1169. * Note: get_new_inode() sets the following on a new inode:
  1170. * i_sb = sb
  1171. * i_no = ino
  1172. * i_flags = sb->s_flags
  1173. * i_state = 0
  1174. * clean_inode(): zero fills and sets
  1175. * i_count = 1
  1176. * i_nlink = 1
  1177. * i_op = NULL;
  1178. */
  1179. bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
  1180. if (!bh) {
  1181. udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
  1182. return -EIO;
  1183. }
  1184. if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
  1185. ident != TAG_IDENT_USE) {
  1186. udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
  1187. inode->i_ino, ident);
  1188. goto out;
  1189. }
  1190. fe = (struct fileEntry *)bh->b_data;
  1191. efe = (struct extendedFileEntry *)bh->b_data;
  1192. if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
  1193. struct buffer_head *ibh;
  1194. ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
  1195. if (ident == TAG_IDENT_IE && ibh) {
  1196. struct kernel_lb_addr loc;
  1197. struct indirectEntry *ie;
  1198. ie = (struct indirectEntry *)ibh->b_data;
  1199. loc = lelb_to_cpu(ie->indirectICB.extLocation);
  1200. if (ie->indirectICB.extLength) {
  1201. brelse(ibh);
  1202. memcpy(&iinfo->i_location, &loc,
  1203. sizeof(struct kernel_lb_addr));
  1204. if (++indirections > UDF_MAX_ICB_NESTING) {
  1205. udf_err(inode->i_sb,
  1206. "too many ICBs in ICB hierarchy"
  1207. " (max %d supported)\n",
  1208. UDF_MAX_ICB_NESTING);
  1209. goto out;
  1210. }
  1211. brelse(bh);
  1212. goto reread;
  1213. }
  1214. }
  1215. brelse(ibh);
  1216. } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
  1217. udf_err(inode->i_sb, "unsupported strategy type: %u\n",
  1218. le16_to_cpu(fe->icbTag.strategyType));
  1219. goto out;
  1220. }
  1221. if (fe->icbTag.strategyType == cpu_to_le16(4))
  1222. iinfo->i_strat4096 = 0;
  1223. else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
  1224. iinfo->i_strat4096 = 1;
  1225. iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
  1226. ICBTAG_FLAG_AD_MASK;
  1227. iinfo->i_unique = 0;
  1228. iinfo->i_lenEAttr = 0;
  1229. iinfo->i_lenExtents = 0;
  1230. iinfo->i_lenAlloc = 0;
  1231. iinfo->i_next_alloc_block = 0;
  1232. iinfo->i_next_alloc_goal = 0;
  1233. if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
  1234. iinfo->i_efe = 1;
  1235. iinfo->i_use = 0;
  1236. ret = udf_alloc_i_data(inode, bs -
  1237. sizeof(struct extendedFileEntry));
  1238. if (ret)
  1239. goto out;
  1240. memcpy(iinfo->i_ext.i_data,
  1241. bh->b_data + sizeof(struct extendedFileEntry),
  1242. bs - sizeof(struct extendedFileEntry));
  1243. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
  1244. iinfo->i_efe = 0;
  1245. iinfo->i_use = 0;
  1246. ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
  1247. if (ret)
  1248. goto out;
  1249. memcpy(iinfo->i_ext.i_data,
  1250. bh->b_data + sizeof(struct fileEntry),
  1251. bs - sizeof(struct fileEntry));
  1252. } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
  1253. iinfo->i_efe = 0;
  1254. iinfo->i_use = 1;
  1255. iinfo->i_lenAlloc = le32_to_cpu(
  1256. ((struct unallocSpaceEntry *)bh->b_data)->
  1257. lengthAllocDescs);
  1258. ret = udf_alloc_i_data(inode, bs -
  1259. sizeof(struct unallocSpaceEntry));
  1260. if (ret)
  1261. goto out;
  1262. memcpy(iinfo->i_ext.i_data,
  1263. bh->b_data + sizeof(struct unallocSpaceEntry),
  1264. bs - sizeof(struct unallocSpaceEntry));
  1265. return 0;
  1266. }
  1267. ret = -EIO;
  1268. read_lock(&sbi->s_cred_lock);
  1269. uid = le32_to_cpu(fe->uid);
  1270. if (uid == UDF_INVALID_ID ||
  1271. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
  1272. inode->i_uid = sbi->s_uid;
  1273. else
  1274. i_uid_write(inode, uid);
  1275. gid = le32_to_cpu(fe->gid);
  1276. if (gid == UDF_INVALID_ID ||
  1277. UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
  1278. inode->i_gid = sbi->s_gid;
  1279. else
  1280. i_gid_write(inode, gid);
  1281. if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
  1282. sbi->s_fmode != UDF_INVALID_MODE)
  1283. inode->i_mode = sbi->s_fmode;
  1284. else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
  1285. sbi->s_dmode != UDF_INVALID_MODE)
  1286. inode->i_mode = sbi->s_dmode;
  1287. else
  1288. inode->i_mode = udf_convert_permissions(fe);
  1289. inode->i_mode &= ~sbi->s_umask;
  1290. read_unlock(&sbi->s_cred_lock);
  1291. link_count = le16_to_cpu(fe->fileLinkCount);
  1292. if (!link_count) {
  1293. if (!hidden_inode) {
  1294. ret = -ESTALE;
  1295. goto out;
  1296. }
  1297. link_count = 1;
  1298. }
  1299. set_nlink(inode, link_count);
  1300. inode->i_size = le64_to_cpu(fe->informationLength);
  1301. iinfo->i_lenExtents = inode->i_size;
  1302. if (iinfo->i_efe == 0) {
  1303. inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
  1304. (inode->i_sb->s_blocksize_bits - 9);
  1305. udf_disk_stamp_to_time(&ts, fe->accessTime);
  1306. inode->i_atime = timespec_to_timespec64(ts);
  1307. udf_disk_stamp_to_time(&ts, fe->modificationTime);
  1308. inode->i_mtime = timespec_to_timespec64(ts);
  1309. udf_disk_stamp_to_time(&ts, fe->attrTime);
  1310. inode->i_ctime = timespec_to_timespec64(ts);
  1311. iinfo->i_unique = le64_to_cpu(fe->uniqueID);
  1312. iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
  1313. iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
  1314. iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
  1315. } else {
  1316. inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
  1317. (inode->i_sb->s_blocksize_bits - 9);
  1318. udf_disk_stamp_to_time(&ts, efe->accessTime);
  1319. inode->i_atime = timespec_to_timespec64(ts);
  1320. udf_disk_stamp_to_time(&ts, efe->modificationTime);
  1321. inode->i_mtime = timespec_to_timespec64(ts);
  1322. udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
  1323. udf_disk_stamp_to_time(&ts, efe->attrTime);
  1324. inode->i_ctime = timespec_to_timespec64(ts);
  1325. iinfo->i_unique = le64_to_cpu(efe->uniqueID);
  1326. iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
  1327. iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
  1328. iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
  1329. }
  1330. inode->i_generation = iinfo->i_unique;
  1331. /*
  1332. * Sanity check length of allocation descriptors and extended attrs to
  1333. * avoid integer overflows
  1334. */
  1335. if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
  1336. goto out;
  1337. /* Now do exact checks */
  1338. if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
  1339. goto out;
  1340. /* Sanity checks for files in ICB so that we don't get confused later */
  1341. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  1342. /*
  1343. * For file in ICB data is stored in allocation descriptor
  1344. * so sizes should match
  1345. */
  1346. if (iinfo->i_lenAlloc != inode->i_size)
  1347. goto out;
  1348. /* File in ICB has to fit in there... */
  1349. if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
  1350. goto out;
  1351. }
  1352. switch (fe->icbTag.fileType) {
  1353. case ICBTAG_FILE_TYPE_DIRECTORY:
  1354. inode->i_op = &udf_dir_inode_operations;
  1355. inode->i_fop = &udf_dir_operations;
  1356. inode->i_mode |= S_IFDIR;
  1357. inc_nlink(inode);
  1358. break;
  1359. case ICBTAG_FILE_TYPE_REALTIME:
  1360. case ICBTAG_FILE_TYPE_REGULAR:
  1361. case ICBTAG_FILE_TYPE_UNDEF:
  1362. case ICBTAG_FILE_TYPE_VAT20:
  1363. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1364. inode->i_data.a_ops = &udf_adinicb_aops;
  1365. else
  1366. inode->i_data.a_ops = &udf_aops;
  1367. inode->i_op = &udf_file_inode_operations;
  1368. inode->i_fop = &udf_file_operations;
  1369. inode->i_mode |= S_IFREG;
  1370. break;
  1371. case ICBTAG_FILE_TYPE_BLOCK:
  1372. inode->i_mode |= S_IFBLK;
  1373. break;
  1374. case ICBTAG_FILE_TYPE_CHAR:
  1375. inode->i_mode |= S_IFCHR;
  1376. break;
  1377. case ICBTAG_FILE_TYPE_FIFO:
  1378. init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
  1379. break;
  1380. case ICBTAG_FILE_TYPE_SOCKET:
  1381. init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
  1382. break;
  1383. case ICBTAG_FILE_TYPE_SYMLINK:
  1384. inode->i_data.a_ops = &udf_symlink_aops;
  1385. inode->i_op = &udf_symlink_inode_operations;
  1386. inode_nohighmem(inode);
  1387. inode->i_mode = S_IFLNK | 0777;
  1388. break;
  1389. case ICBTAG_FILE_TYPE_MAIN:
  1390. udf_debug("METADATA FILE-----\n");
  1391. break;
  1392. case ICBTAG_FILE_TYPE_MIRROR:
  1393. udf_debug("METADATA MIRROR FILE-----\n");
  1394. break;
  1395. case ICBTAG_FILE_TYPE_BITMAP:
  1396. udf_debug("METADATA BITMAP FILE-----\n");
  1397. break;
  1398. default:
  1399. udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
  1400. inode->i_ino, fe->icbTag.fileType);
  1401. goto out;
  1402. }
  1403. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1404. struct deviceSpec *dsea =
  1405. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1406. if (dsea) {
  1407. init_special_inode(inode, inode->i_mode,
  1408. MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
  1409. le32_to_cpu(dsea->minorDeviceIdent)));
  1410. /* Developer ID ??? */
  1411. } else
  1412. goto out;
  1413. }
  1414. ret = 0;
  1415. out:
  1416. brelse(bh);
  1417. return ret;
  1418. }
  1419. static int udf_alloc_i_data(struct inode *inode, size_t size)
  1420. {
  1421. struct udf_inode_info *iinfo = UDF_I(inode);
  1422. iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
  1423. if (!iinfo->i_ext.i_data)
  1424. return -ENOMEM;
  1425. return 0;
  1426. }
  1427. static umode_t udf_convert_permissions(struct fileEntry *fe)
  1428. {
  1429. umode_t mode;
  1430. uint32_t permissions;
  1431. uint32_t flags;
  1432. permissions = le32_to_cpu(fe->permissions);
  1433. flags = le16_to_cpu(fe->icbTag.flags);
  1434. mode = ((permissions) & 0007) |
  1435. ((permissions >> 2) & 0070) |
  1436. ((permissions >> 4) & 0700) |
  1437. ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
  1438. ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
  1439. ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
  1440. return mode;
  1441. }
  1442. int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
  1443. {
  1444. return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  1445. }
  1446. static int udf_sync_inode(struct inode *inode)
  1447. {
  1448. return udf_update_inode(inode, 1);
  1449. }
  1450. static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
  1451. {
  1452. if (iinfo->i_crtime.tv_sec > time.tv_sec ||
  1453. (iinfo->i_crtime.tv_sec == time.tv_sec &&
  1454. iinfo->i_crtime.tv_nsec > time.tv_nsec))
  1455. iinfo->i_crtime = time;
  1456. }
  1457. static int udf_update_inode(struct inode *inode, int do_sync)
  1458. {
  1459. struct buffer_head *bh = NULL;
  1460. struct fileEntry *fe;
  1461. struct extendedFileEntry *efe;
  1462. uint64_t lb_recorded;
  1463. uint32_t udfperms;
  1464. uint16_t icbflags;
  1465. uint16_t crclen;
  1466. int err = 0;
  1467. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  1468. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  1469. struct udf_inode_info *iinfo = UDF_I(inode);
  1470. bh = udf_tgetblk(inode->i_sb,
  1471. udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
  1472. if (!bh) {
  1473. udf_debug("getblk failure\n");
  1474. return -EIO;
  1475. }
  1476. lock_buffer(bh);
  1477. memset(bh->b_data, 0, inode->i_sb->s_blocksize);
  1478. fe = (struct fileEntry *)bh->b_data;
  1479. efe = (struct extendedFileEntry *)bh->b_data;
  1480. if (iinfo->i_use) {
  1481. struct unallocSpaceEntry *use =
  1482. (struct unallocSpaceEntry *)bh->b_data;
  1483. use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1484. memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
  1485. iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
  1486. sizeof(struct unallocSpaceEntry));
  1487. use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
  1488. crclen = sizeof(struct unallocSpaceEntry);
  1489. goto finish;
  1490. }
  1491. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
  1492. fe->uid = cpu_to_le32(UDF_INVALID_ID);
  1493. else
  1494. fe->uid = cpu_to_le32(i_uid_read(inode));
  1495. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
  1496. fe->gid = cpu_to_le32(UDF_INVALID_ID);
  1497. else
  1498. fe->gid = cpu_to_le32(i_gid_read(inode));
  1499. udfperms = ((inode->i_mode & 0007)) |
  1500. ((inode->i_mode & 0070) << 2) |
  1501. ((inode->i_mode & 0700) << 4);
  1502. udfperms |= (le32_to_cpu(fe->permissions) &
  1503. (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
  1504. FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
  1505. FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
  1506. fe->permissions = cpu_to_le32(udfperms);
  1507. if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
  1508. fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
  1509. else
  1510. fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
  1511. fe->informationLength = cpu_to_le64(inode->i_size);
  1512. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  1513. struct regid *eid;
  1514. struct deviceSpec *dsea =
  1515. (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
  1516. if (!dsea) {
  1517. dsea = (struct deviceSpec *)
  1518. udf_add_extendedattr(inode,
  1519. sizeof(struct deviceSpec) +
  1520. sizeof(struct regid), 12, 0x3);
  1521. dsea->attrType = cpu_to_le32(12);
  1522. dsea->attrSubtype = 1;
  1523. dsea->attrLength = cpu_to_le32(
  1524. sizeof(struct deviceSpec) +
  1525. sizeof(struct regid));
  1526. dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
  1527. }
  1528. eid = (struct regid *)dsea->impUse;
  1529. memset(eid, 0, sizeof(*eid));
  1530. strcpy(eid->ident, UDF_ID_DEVELOPER);
  1531. eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
  1532. eid->identSuffix[1] = UDF_OS_ID_LINUX;
  1533. dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
  1534. dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
  1535. }
  1536. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
  1537. lb_recorded = 0; /* No extents => no blocks! */
  1538. else
  1539. lb_recorded =
  1540. (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
  1541. (blocksize_bits - 9);
  1542. if (iinfo->i_efe == 0) {
  1543. memcpy(bh->b_data + sizeof(struct fileEntry),
  1544. iinfo->i_ext.i_data,
  1545. inode->i_sb->s_blocksize - sizeof(struct fileEntry));
  1546. fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1547. udf_time_to_disk_stamp(&fe->accessTime,
  1548. timespec64_to_timespec(inode->i_atime));
  1549. udf_time_to_disk_stamp(&fe->modificationTime,
  1550. timespec64_to_timespec(inode->i_mtime));
  1551. udf_time_to_disk_stamp(&fe->attrTime,
  1552. timespec64_to_timespec(inode->i_ctime));
  1553. memset(&(fe->impIdent), 0, sizeof(struct regid));
  1554. strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
  1555. fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1556. fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1557. fe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1558. fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1559. fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1560. fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1561. fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
  1562. crclen = sizeof(struct fileEntry);
  1563. } else {
  1564. memcpy(bh->b_data + sizeof(struct extendedFileEntry),
  1565. iinfo->i_ext.i_data,
  1566. inode->i_sb->s_blocksize -
  1567. sizeof(struct extendedFileEntry));
  1568. efe->objectSize = cpu_to_le64(inode->i_size);
  1569. efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
  1570. udf_adjust_time(iinfo, timespec64_to_timespec(inode->i_atime));
  1571. udf_adjust_time(iinfo, timespec64_to_timespec(inode->i_mtime));
  1572. udf_adjust_time(iinfo, timespec64_to_timespec(inode->i_ctime));
  1573. udf_time_to_disk_stamp(&efe->accessTime,
  1574. timespec64_to_timespec(inode->i_atime));
  1575. udf_time_to_disk_stamp(&efe->modificationTime,
  1576. timespec64_to_timespec(inode->i_mtime));
  1577. udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
  1578. udf_time_to_disk_stamp(&efe->attrTime,
  1579. timespec64_to_timespec(inode->i_ctime));
  1580. memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
  1581. strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
  1582. efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
  1583. efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
  1584. efe->uniqueID = cpu_to_le64(iinfo->i_unique);
  1585. efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
  1586. efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
  1587. efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
  1588. efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
  1589. crclen = sizeof(struct extendedFileEntry);
  1590. }
  1591. finish:
  1592. if (iinfo->i_strat4096) {
  1593. fe->icbTag.strategyType = cpu_to_le16(4096);
  1594. fe->icbTag.strategyParameter = cpu_to_le16(1);
  1595. fe->icbTag.numEntries = cpu_to_le16(2);
  1596. } else {
  1597. fe->icbTag.strategyType = cpu_to_le16(4);
  1598. fe->icbTag.numEntries = cpu_to_le16(1);
  1599. }
  1600. if (iinfo->i_use)
  1601. fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
  1602. else if (S_ISDIR(inode->i_mode))
  1603. fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
  1604. else if (S_ISREG(inode->i_mode))
  1605. fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
  1606. else if (S_ISLNK(inode->i_mode))
  1607. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
  1608. else if (S_ISBLK(inode->i_mode))
  1609. fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
  1610. else if (S_ISCHR(inode->i_mode))
  1611. fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
  1612. else if (S_ISFIFO(inode->i_mode))
  1613. fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
  1614. else if (S_ISSOCK(inode->i_mode))
  1615. fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
  1616. icbflags = iinfo->i_alloc_type |
  1617. ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
  1618. ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
  1619. ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
  1620. (le16_to_cpu(fe->icbTag.flags) &
  1621. ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
  1622. ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
  1623. fe->icbTag.flags = cpu_to_le16(icbflags);
  1624. if (sbi->s_udfrev >= 0x0200)
  1625. fe->descTag.descVersion = cpu_to_le16(3);
  1626. else
  1627. fe->descTag.descVersion = cpu_to_le16(2);
  1628. fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
  1629. fe->descTag.tagLocation = cpu_to_le32(
  1630. iinfo->i_location.logicalBlockNum);
  1631. crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
  1632. fe->descTag.descCRCLength = cpu_to_le16(crclen);
  1633. fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
  1634. crclen));
  1635. fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
  1636. set_buffer_uptodate(bh);
  1637. unlock_buffer(bh);
  1638. /* write the data blocks */
  1639. mark_buffer_dirty(bh);
  1640. if (do_sync) {
  1641. sync_dirty_buffer(bh);
  1642. if (buffer_write_io_error(bh)) {
  1643. udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
  1644. inode->i_ino);
  1645. err = -EIO;
  1646. }
  1647. }
  1648. brelse(bh);
  1649. return err;
  1650. }
  1651. struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
  1652. bool hidden_inode)
  1653. {
  1654. unsigned long block = udf_get_lb_pblock(sb, ino, 0);
  1655. struct inode *inode = iget_locked(sb, block);
  1656. int err;
  1657. if (!inode)
  1658. return ERR_PTR(-ENOMEM);
  1659. if (!(inode->i_state & I_NEW))
  1660. return inode;
  1661. memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
  1662. err = udf_read_inode(inode, hidden_inode);
  1663. if (err < 0) {
  1664. iget_failed(inode);
  1665. return ERR_PTR(err);
  1666. }
  1667. unlock_new_inode(inode);
  1668. return inode;
  1669. }
  1670. int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
  1671. struct extent_position *epos)
  1672. {
  1673. struct super_block *sb = inode->i_sb;
  1674. struct buffer_head *bh;
  1675. struct allocExtDesc *aed;
  1676. struct extent_position nepos;
  1677. struct kernel_lb_addr neloc;
  1678. int ver, adsize;
  1679. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1680. adsize = sizeof(struct short_ad);
  1681. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1682. adsize = sizeof(struct long_ad);
  1683. else
  1684. return -EIO;
  1685. neloc.logicalBlockNum = block;
  1686. neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
  1687. bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
  1688. if (!bh)
  1689. return -EIO;
  1690. lock_buffer(bh);
  1691. memset(bh->b_data, 0x00, sb->s_blocksize);
  1692. set_buffer_uptodate(bh);
  1693. unlock_buffer(bh);
  1694. mark_buffer_dirty_inode(bh, inode);
  1695. aed = (struct allocExtDesc *)(bh->b_data);
  1696. if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
  1697. aed->previousAllocExtLocation =
  1698. cpu_to_le32(epos->block.logicalBlockNum);
  1699. }
  1700. aed->lengthAllocDescs = cpu_to_le32(0);
  1701. if (UDF_SB(sb)->s_udfrev >= 0x0200)
  1702. ver = 3;
  1703. else
  1704. ver = 2;
  1705. udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
  1706. sizeof(struct tag));
  1707. nepos.block = neloc;
  1708. nepos.offset = sizeof(struct allocExtDesc);
  1709. nepos.bh = bh;
  1710. /*
  1711. * Do we have to copy current last extent to make space for indirect
  1712. * one?
  1713. */
  1714. if (epos->offset + adsize > sb->s_blocksize) {
  1715. struct kernel_lb_addr cp_loc;
  1716. uint32_t cp_len;
  1717. int cp_type;
  1718. epos->offset -= adsize;
  1719. cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
  1720. cp_len |= ((uint32_t)cp_type) << 30;
  1721. __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
  1722. udf_write_aext(inode, epos, &nepos.block,
  1723. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
  1724. } else {
  1725. __udf_add_aext(inode, epos, &nepos.block,
  1726. sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
  1727. }
  1728. brelse(epos->bh);
  1729. *epos = nepos;
  1730. return 0;
  1731. }
  1732. /*
  1733. * Append extent at the given position - should be the first free one in inode
  1734. * / indirect extent. This function assumes there is enough space in the inode
  1735. * or indirect extent. Use udf_add_aext() if you didn't check for this before.
  1736. */
  1737. int __udf_add_aext(struct inode *inode, struct extent_position *epos,
  1738. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1739. {
  1740. struct udf_inode_info *iinfo = UDF_I(inode);
  1741. struct allocExtDesc *aed;
  1742. int adsize;
  1743. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1744. adsize = sizeof(struct short_ad);
  1745. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1746. adsize = sizeof(struct long_ad);
  1747. else
  1748. return -EIO;
  1749. if (!epos->bh) {
  1750. WARN_ON(iinfo->i_lenAlloc !=
  1751. epos->offset - udf_file_entry_alloc_offset(inode));
  1752. } else {
  1753. aed = (struct allocExtDesc *)epos->bh->b_data;
  1754. WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
  1755. epos->offset - sizeof(struct allocExtDesc));
  1756. WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
  1757. }
  1758. udf_write_aext(inode, epos, eloc, elen, inc);
  1759. if (!epos->bh) {
  1760. iinfo->i_lenAlloc += adsize;
  1761. mark_inode_dirty(inode);
  1762. } else {
  1763. aed = (struct allocExtDesc *)epos->bh->b_data;
  1764. le32_add_cpu(&aed->lengthAllocDescs, adsize);
  1765. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1766. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1767. udf_update_tag(epos->bh->b_data,
  1768. epos->offset + (inc ? 0 : adsize));
  1769. else
  1770. udf_update_tag(epos->bh->b_data,
  1771. sizeof(struct allocExtDesc));
  1772. mark_buffer_dirty_inode(epos->bh, inode);
  1773. }
  1774. return 0;
  1775. }
  1776. /*
  1777. * Append extent at given position - should be the first free one in inode
  1778. * / indirect extent. Takes care of allocating and linking indirect blocks.
  1779. */
  1780. int udf_add_aext(struct inode *inode, struct extent_position *epos,
  1781. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1782. {
  1783. int adsize;
  1784. struct super_block *sb = inode->i_sb;
  1785. if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1786. adsize = sizeof(struct short_ad);
  1787. else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1788. adsize = sizeof(struct long_ad);
  1789. else
  1790. return -EIO;
  1791. if (epos->offset + (2 * adsize) > sb->s_blocksize) {
  1792. int err;
  1793. udf_pblk_t new_block;
  1794. new_block = udf_new_block(sb, NULL,
  1795. epos->block.partitionReferenceNum,
  1796. epos->block.logicalBlockNum, &err);
  1797. if (!new_block)
  1798. return -ENOSPC;
  1799. err = udf_setup_indirect_aext(inode, new_block, epos);
  1800. if (err)
  1801. return err;
  1802. }
  1803. return __udf_add_aext(inode, epos, eloc, elen, inc);
  1804. }
  1805. void udf_write_aext(struct inode *inode, struct extent_position *epos,
  1806. struct kernel_lb_addr *eloc, uint32_t elen, int inc)
  1807. {
  1808. int adsize;
  1809. uint8_t *ptr;
  1810. struct short_ad *sad;
  1811. struct long_ad *lad;
  1812. struct udf_inode_info *iinfo = UDF_I(inode);
  1813. if (!epos->bh)
  1814. ptr = iinfo->i_ext.i_data + epos->offset -
  1815. udf_file_entry_alloc_offset(inode) +
  1816. iinfo->i_lenEAttr;
  1817. else
  1818. ptr = epos->bh->b_data + epos->offset;
  1819. switch (iinfo->i_alloc_type) {
  1820. case ICBTAG_FLAG_AD_SHORT:
  1821. sad = (struct short_ad *)ptr;
  1822. sad->extLength = cpu_to_le32(elen);
  1823. sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
  1824. adsize = sizeof(struct short_ad);
  1825. break;
  1826. case ICBTAG_FLAG_AD_LONG:
  1827. lad = (struct long_ad *)ptr;
  1828. lad->extLength = cpu_to_le32(elen);
  1829. lad->extLocation = cpu_to_lelb(*eloc);
  1830. memset(lad->impUse, 0x00, sizeof(lad->impUse));
  1831. adsize = sizeof(struct long_ad);
  1832. break;
  1833. default:
  1834. return;
  1835. }
  1836. if (epos->bh) {
  1837. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1838. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
  1839. struct allocExtDesc *aed =
  1840. (struct allocExtDesc *)epos->bh->b_data;
  1841. udf_update_tag(epos->bh->b_data,
  1842. le32_to_cpu(aed->lengthAllocDescs) +
  1843. sizeof(struct allocExtDesc));
  1844. }
  1845. mark_buffer_dirty_inode(epos->bh, inode);
  1846. } else {
  1847. mark_inode_dirty(inode);
  1848. }
  1849. if (inc)
  1850. epos->offset += adsize;
  1851. }
  1852. /*
  1853. * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
  1854. * someone does some weird stuff.
  1855. */
  1856. #define UDF_MAX_INDIR_EXTS 16
  1857. int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
  1858. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1859. {
  1860. int8_t etype;
  1861. unsigned int indirections = 0;
  1862. while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
  1863. (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
  1864. udf_pblk_t block;
  1865. if (++indirections > UDF_MAX_INDIR_EXTS) {
  1866. udf_err(inode->i_sb,
  1867. "too many indirect extents in inode %lu\n",
  1868. inode->i_ino);
  1869. return -1;
  1870. }
  1871. epos->block = *eloc;
  1872. epos->offset = sizeof(struct allocExtDesc);
  1873. brelse(epos->bh);
  1874. block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
  1875. epos->bh = udf_tread(inode->i_sb, block);
  1876. if (!epos->bh) {
  1877. udf_debug("reading block %u failed!\n", block);
  1878. return -1;
  1879. }
  1880. }
  1881. return etype;
  1882. }
  1883. int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
  1884. struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
  1885. {
  1886. int alen;
  1887. int8_t etype;
  1888. uint8_t *ptr;
  1889. struct short_ad *sad;
  1890. struct long_ad *lad;
  1891. struct udf_inode_info *iinfo = UDF_I(inode);
  1892. if (!epos->bh) {
  1893. if (!epos->offset)
  1894. epos->offset = udf_file_entry_alloc_offset(inode);
  1895. ptr = iinfo->i_ext.i_data + epos->offset -
  1896. udf_file_entry_alloc_offset(inode) +
  1897. iinfo->i_lenEAttr;
  1898. alen = udf_file_entry_alloc_offset(inode) +
  1899. iinfo->i_lenAlloc;
  1900. } else {
  1901. if (!epos->offset)
  1902. epos->offset = sizeof(struct allocExtDesc);
  1903. ptr = epos->bh->b_data + epos->offset;
  1904. alen = sizeof(struct allocExtDesc) +
  1905. le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
  1906. lengthAllocDescs);
  1907. }
  1908. switch (iinfo->i_alloc_type) {
  1909. case ICBTAG_FLAG_AD_SHORT:
  1910. sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
  1911. if (!sad)
  1912. return -1;
  1913. etype = le32_to_cpu(sad->extLength) >> 30;
  1914. eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
  1915. eloc->partitionReferenceNum =
  1916. iinfo->i_location.partitionReferenceNum;
  1917. *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1918. break;
  1919. case ICBTAG_FLAG_AD_LONG:
  1920. lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
  1921. if (!lad)
  1922. return -1;
  1923. etype = le32_to_cpu(lad->extLength) >> 30;
  1924. *eloc = lelb_to_cpu(lad->extLocation);
  1925. *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
  1926. break;
  1927. default:
  1928. udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
  1929. return -1;
  1930. }
  1931. return etype;
  1932. }
  1933. static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
  1934. struct kernel_lb_addr neloc, uint32_t nelen)
  1935. {
  1936. struct kernel_lb_addr oeloc;
  1937. uint32_t oelen;
  1938. int8_t etype;
  1939. if (epos.bh)
  1940. get_bh(epos.bh);
  1941. while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
  1942. udf_write_aext(inode, &epos, &neloc, nelen, 1);
  1943. neloc = oeloc;
  1944. nelen = (etype << 30) | oelen;
  1945. }
  1946. udf_add_aext(inode, &epos, &neloc, nelen, 1);
  1947. brelse(epos.bh);
  1948. return (nelen >> 30);
  1949. }
  1950. int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
  1951. struct kernel_lb_addr eloc, uint32_t elen)
  1952. {
  1953. struct extent_position oepos;
  1954. int adsize;
  1955. int8_t etype;
  1956. struct allocExtDesc *aed;
  1957. struct udf_inode_info *iinfo;
  1958. if (epos.bh) {
  1959. get_bh(epos.bh);
  1960. get_bh(epos.bh);
  1961. }
  1962. iinfo = UDF_I(inode);
  1963. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
  1964. adsize = sizeof(struct short_ad);
  1965. else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
  1966. adsize = sizeof(struct long_ad);
  1967. else
  1968. adsize = 0;
  1969. oepos = epos;
  1970. if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
  1971. return -1;
  1972. while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
  1973. udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
  1974. if (oepos.bh != epos.bh) {
  1975. oepos.block = epos.block;
  1976. brelse(oepos.bh);
  1977. get_bh(epos.bh);
  1978. oepos.bh = epos.bh;
  1979. oepos.offset = epos.offset - adsize;
  1980. }
  1981. }
  1982. memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
  1983. elen = 0;
  1984. if (epos.bh != oepos.bh) {
  1985. udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
  1986. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1987. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  1988. if (!oepos.bh) {
  1989. iinfo->i_lenAlloc -= (adsize * 2);
  1990. mark_inode_dirty(inode);
  1991. } else {
  1992. aed = (struct allocExtDesc *)oepos.bh->b_data;
  1993. le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
  1994. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  1995. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  1996. udf_update_tag(oepos.bh->b_data,
  1997. oepos.offset - (2 * adsize));
  1998. else
  1999. udf_update_tag(oepos.bh->b_data,
  2000. sizeof(struct allocExtDesc));
  2001. mark_buffer_dirty_inode(oepos.bh, inode);
  2002. }
  2003. } else {
  2004. udf_write_aext(inode, &oepos, &eloc, elen, 1);
  2005. if (!oepos.bh) {
  2006. iinfo->i_lenAlloc -= adsize;
  2007. mark_inode_dirty(inode);
  2008. } else {
  2009. aed = (struct allocExtDesc *)oepos.bh->b_data;
  2010. le32_add_cpu(&aed->lengthAllocDescs, -adsize);
  2011. if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
  2012. UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
  2013. udf_update_tag(oepos.bh->b_data,
  2014. epos.offset - adsize);
  2015. else
  2016. udf_update_tag(oepos.bh->b_data,
  2017. sizeof(struct allocExtDesc));
  2018. mark_buffer_dirty_inode(oepos.bh, inode);
  2019. }
  2020. }
  2021. brelse(epos.bh);
  2022. brelse(oepos.bh);
  2023. return (elen >> 30);
  2024. }
  2025. int8_t inode_bmap(struct inode *inode, sector_t block,
  2026. struct extent_position *pos, struct kernel_lb_addr *eloc,
  2027. uint32_t *elen, sector_t *offset)
  2028. {
  2029. unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
  2030. loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
  2031. int8_t etype;
  2032. struct udf_inode_info *iinfo;
  2033. iinfo = UDF_I(inode);
  2034. if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
  2035. pos->offset = 0;
  2036. pos->block = iinfo->i_location;
  2037. pos->bh = NULL;
  2038. }
  2039. *elen = 0;
  2040. do {
  2041. etype = udf_next_aext(inode, pos, eloc, elen, 1);
  2042. if (etype == -1) {
  2043. *offset = (bcount - lbcount) >> blocksize_bits;
  2044. iinfo->i_lenExtents = lbcount;
  2045. return -1;
  2046. }
  2047. lbcount += *elen;
  2048. } while (lbcount <= bcount);
  2049. /* update extent cache */
  2050. udf_update_extent_cache(inode, lbcount - *elen, pos);
  2051. *offset = (bcount + *elen - lbcount) >> blocksize_bits;
  2052. return etype;
  2053. }
  2054. udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
  2055. {
  2056. struct kernel_lb_addr eloc;
  2057. uint32_t elen;
  2058. sector_t offset;
  2059. struct extent_position epos = {};
  2060. udf_pblk_t ret;
  2061. down_read(&UDF_I(inode)->i_data_sem);
  2062. if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
  2063. (EXT_RECORDED_ALLOCATED >> 30))
  2064. ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
  2065. else
  2066. ret = 0;
  2067. up_read(&UDF_I(inode)->i_data_sem);
  2068. brelse(epos.bh);
  2069. if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
  2070. return udf_fixed_to_variable(ret);
  2071. else
  2072. return ret;
  2073. }