inode.c 65 KB

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