inode.c 66 KB

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